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

/**
 * Returns 'billingAddress' specified in the order.
 */
function useBillingAddress() {
  const api = useApi();
  const extensionTarget = api.extension.target;
  if (!('billingAddress' in api)) {
    throw new ExtensionHasNoFieldError('billingAddress', extensionTarget);
  }
  const billingAddress = api.billingAddress;
  if (!billingAddress) {
    throw new ScopeNotGrantedError('Using billing address requires having billing address permissions granted to your app.');
  }
  return useSubscription(billingAddress);
}

export { useBillingAddress };
