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

/**
 * Returns the current discount codes applied to the cart, and automatically re-renders
 * your component if discount codes are added or removed.
 */
function useDiscountCodes() {
  const api = useApi();
  const extensionTarget = api.extension.target;
  if (!('discountCodes' in api)) {
    throw new ExtensionHasNoFieldError('discountCodes', extensionTarget);
  }
  return useSubscription(api.discountCodes);
}

/**
 * Returns the current discount allocations applied to the cart, and automatically re-renders
 * your component if discount allocations changed.
 */
function useDiscountAllocations() {
  const api = useApi();
  const extensionTarget = api.extension.target;
  if (!('discountAllocations' in api)) {
    throw new ExtensionHasNoFieldError('discountAllocations', extensionTarget);
  }
  return useSubscription(api.discountAllocations);
}

export { useDiscountAllocations, useDiscountCodes };
