import { __, _n, sprintf } from '@wordpress/i18n';
export const convertAmount = (amount, currency) => {
  return ['bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw'].includes(currency) ? amount : amount / 100;
};
export const getHumanDiscount = (coupon) => {
  if (coupon.amount_off && coupon.currency) {
    return getFormattedPrice({ amount: coupon.amount_off, currency: coupon.currency });
  }
  if (coupon.percent_off) {
    // TODO: Translators.
    return `${coupon.percent_off | 0}% off`;
  }
  return '';
};
export const getFormattedPrice = ({ amount, currency }) => {
  const converted = convertAmount(amount, currency);
  return `${new Intl.NumberFormat(undefined, {
    style: 'currency',
    currency: currency,
  }).format(parseFloat(converted.toFixed(2)))}`;
};
// get the currency symbol for a currency code.
export const getCurrencySymbol = (code = 'usd') => {
  const [currency] = new Intl.NumberFormat(undefined, {
    style: 'currency',
    currency: code,
  }).formatToParts();
  return currency === null || currency === void 0 ? void 0 : currency.value;
};
export const translateInterval = (amount, interval, prefix = __('every', 'surecart'), fallback = __('once', 'surecart'), showSingle = false) => {
  switch (interval) {
    case 'day':
      return `${prefix} ${sprintf(showSingle ? _n('%d day', '%d days', amount, 'surecart') : _n('day', '%d days', amount, 'surecart'), amount)}`;
    case 'week':
      return `${prefix} ${sprintf(showSingle ? _n('%d week', '%d weeks', amount, 'surecart') : _n('week', '%d weeks', amount, 'surecart'), amount)}`;
    case 'month':
      return `${prefix} ${sprintf(showSingle ? _n('%d month', '%d months', amount, 'surecart') : _n('month', '%d months', amount, 'surecart'), amount)}`;
    case 'year':
      return `${prefix} ${sprintf(showSingle ? _n('%d year', '%d years', amount, 'surecart') : _n('year', '%d years', amount, 'surecart'), amount)}`;
    default:
      return fallback;
  }
};
export const intervalString = (price, options = {}) => {
  if (!price) {
    return '';
  }
  const { showOnce, labels } = options;
  const { interval = __('every', 'surecart'), period = __('for', 'surecart') } = labels || {};
  return `${intervalCountString(price, interval, !!showOnce ? __('once', 'surecart') : '')} ${periodCountString(price, period)}`;
};
export const intervalCountString = (price, prefix, fallback = __('once', 'surecart')) => {
  if (!price.recurring_interval_count || !price.recurring_interval) {
    return '';
  }
  return translateInterval(price.recurring_interval_count, price.recurring_interval, ` ${prefix}`, fallback);
};
export const periodCountString = (price, prefix, fallback = '') => {
  if (!(price === null || price === void 0 ? void 0 : price.recurring_period_count) || !(price === null || price === void 0 ? void 0 : price.recurring_interval)) {
    return '';
  }
  return translateInterval(((price === null || price === void 0 ? void 0 : price.recurring_period_count) || 0) * (price === null || price === void 0 ? void 0 : price.recurring_interval_count), price === null || price === void 0 ? void 0 : price.recurring_interval, ` ${prefix}`, fallback, true);
};
