'use strict';

const defaultI18n = require('./default-i18n-072e75d7.js');
require('./index-20b57ec7.js');

const convertAmount = (amount, currency) => {
  return ['bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw'].includes(currency) ? amount : amount / 100;
};
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 '';
};
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.
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;
};
const translateInterval = (amount, interval, prefix = defaultI18n.__('every', 'surecart'), fallback = defaultI18n.__('once', 'surecart'), showSingle = false) => {
  switch (interval) {
    case 'day':
      return `${prefix} ${defaultI18n.sprintf(showSingle ? defaultI18n._n('%d day', '%d days', amount, 'surecart') : defaultI18n._n('day', '%d days', amount, 'surecart'), amount)}`;
    case 'week':
      return `${prefix} ${defaultI18n.sprintf(showSingle ? defaultI18n._n('%d week', '%d weeks', amount, 'surecart') : defaultI18n._n('week', '%d weeks', amount, 'surecart'), amount)}`;
    case 'month':
      return `${prefix} ${defaultI18n.sprintf(showSingle ? defaultI18n._n('%d month', '%d months', amount, 'surecart') : defaultI18n._n('month', '%d months', amount, 'surecart'), amount)}`;
    case 'year':
      return `${prefix} ${defaultI18n.sprintf(showSingle ? defaultI18n._n('%d year', '%d years', amount, 'surecart') : defaultI18n._n('year', '%d years', amount, 'surecart'), amount)}`;
    default:
      return fallback;
  }
};
const intervalString = (price, options = {}) => {
  if (!price) {
    return '';
  }
  const { showOnce, labels } = options;
  const { interval = defaultI18n.__('every', 'surecart'), period = defaultI18n.__('for', 'surecart') } = labels || {};
  return `${intervalCountString(price, interval, !!showOnce ? defaultI18n.__('once', 'surecart') : '')} ${periodCountString(price, period)}`;
};
const intervalCountString = (price, prefix, fallback = defaultI18n.__('once', 'surecart')) => {
  if (!price.recurring_interval_count || !price.recurring_interval) {
    return '';
  }
  return translateInterval(price.recurring_interval_count, price.recurring_interval, ` ${prefix}`, fallback);
};
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);
};

exports.getCurrencySymbol = getCurrencySymbol;
exports.getHumanDiscount = getHumanDiscount;
exports.intervalString = intervalString;
exports.translateInterval = translateInterval;
