import { useApi } from './api.mjs';
import { useSubscription } from './subscription.mjs';
import { ExtensionHasNoFieldError } from './errors.mjs';

/**
 * Returns the country associated with either the current order on the order status page
 * or the selected country in the customer account interface,
 * and automatically re-renders your component if the country changes.
 */
function useLocalizationCountry() {
  const api = useApi();
  const extensionTarget = api.extension.target;
  if (!('country' in api.localization)) {
    throw new ExtensionHasNoFieldError('country', extensionTarget);
  }
  return useSubscription(api.localization.country);
}

export { useLocalizationCountry };
