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

/**
 * Returns the currency of the checkout, and automatically re-renders
 * your component if the currency changes.
 */
function useCurrency() {
  const api = useApi();
  const extensionTarget = api.extension.target;
  if (!('currency' in api.localization)) {
    throw new ExtensionHasNoFieldError('currency', extensionTarget);
  }
  return useSubscription(api.localization.currency);
}

export { useCurrency };
