import{__,_n,sprintf}from"@wordpress/i18n";import{zeroDecimalCurrencies}from"./currency";export const convertAmount=(r,e)=>zeroDecimalCurrencies.includes(e)?r:r/100;export const getHumanDiscount=r=>{if((null==r?void 0:r.amount_off)&&(null==r?void 0:r.currency))return getFormattedPrice({amount:r.amount_off,currency:r.currency});if(null==r?void 0:r.percent_off){const e=(0|r.percent_off)+"%";
// translators: %s is the discount percentage (e.g. "10%").
return sprintf(__("%s off","surecart"),e)}return""};export const getFormattedDiscount=r=>(null==r?void 0:r.percent_off)?(0|r.percent_off)+"%":(null==r?void 0:r.amount_off)&&(null==r?void 0:r.currency)?getFormattedPrice({amount:r.amount_off,currency:r.currency}):"";export const getFormattedPrice=({amount:r,currency:e})=>{const t=convertAmount(r,e);return`${new Intl.NumberFormat(void 0,{style:"currency",currency:e}).format(parseFloat(t.toFixed(2)))}`};export const getCurrencySymbol=(r="usd")=>{var e;return null===(e=new Intl.NumberFormat(void 0,{style:"currency",currency:r}).formatToParts().find((r=>"currency"===r.type)))||void 0===e?void 0:e.value};export const translateInterval=(r,e,t=__("every","surecart"),n=__("once","surecart"),a=!1)=>{switch(e){case"day":return`${t} ${sprintf(_n(a?"%d day":"day","%d days",r,"surecart"),r)}`;case"week":return`${t} ${sprintf(_n(a?"%d week":"week","%d weeks",r,"surecart"),r)}`;case"month":return`${t} ${sprintf(_n(a?"%d month":"month","%d months",r,"surecart"),r)}`;case"year":return`${t} ${sprintf(_n(a?"%d year":"year","%d years",r,"surecart"),r)}`;default:return n}};export const translateAbbreviatedInterval=(r,e,t=__("once","surecart"),n=!1)=>{switch(e){case"day":return` / ${sprintf(_n(n?"%d day":"day","%d days",r,"surecart"),r)}`;case"week":return` / ${sprintf(_n(n?"%d wk":"wk","%d wks",r,"surecart"),r)}`;case"month":return` / ${sprintf(n?_n("%d mo","%d months",r,"surecart"):_n("mo","%d mos",r,"surecart"),r)}`;case"year":return` / ${sprintf(_n(n?"%d yr":"yr","%d yrs",r,"surecart"),r)}`;default:return t}};export const intervalString=(r,e={})=>{if(!r)return"";const{showOnce:t,labels:n,abbreviate:a}=e,{interval:o=__("every","surecart")}=n||{};return`${intervalCountString(r,o,t?__("once","surecart"):"",a)} ${periodCountString(r,a)}`};export const intervalCountString=(r,e,t=__("once","surecart"),n=!1)=>r.recurring_interval_count&&r.recurring_interval&&1!==(null==r?void 0:r.recurring_period_count)?n?translateAbbreviatedInterval(r.recurring_interval_count,r.recurring_interval,t):translateInterval(r.recurring_interval_count,r.recurring_interval,` ${e}`,t):"";export const periodCountString=(r,e=!1)=>(null==r?void 0:r.recurring_period_count)&&1!==(null==r?void 0:r.recurring_period_count)?e?`x ${r.recurring_period_count}`:` (${sprintf(_n("%d payment","%d payments",r.recurring_period_count,"surecart"),r.recurring_period_count)})`:"";export const translateRemainingPayments=r=>sprintf(_n("%d payment remaining","%d payments remaining",r,"surecart"),r);export const productNameWithPrice=r=>{var e;return r?`${null===(e=null==r?void 0:r.product)||void 0===e?void 0:e.name} ${(null==r?void 0:r.name)?`— ${r.name}`:""}`:""};export const getHumanDiscountRedeemableStatus=r=>{switch(r){case"invalid":return __("Not valid","surecart");case"expired":return __("Expired","surecart");case"gone":return __("Not available","surecart");case"less_than_min_subtotal_amount":return __("Minimum not met","surecart");case"greater_than_max_subtotal_amount":return __("Order amount exceeds limit","surecart");case"not_applicable":return __("Product(s) not eligible","surecart");case"not_applicable_to_customer":return __("Not eligible","surecart");case"":return"";default:return __("Not redeemable","surecart")}};