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

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

export { useTimezone };
