import state,{onChange,on}from"./store";import{getCheckout,setCheckout}from"../checkouts/mutations";import{onChange as onChangeFormState}from"../form";import{updateFormState}from"../form/mutations";import{speak}from"@wordpress/a11y";import{__,sprintf}from"@wordpress/i18n";import{getFormattedPrice,getHumanDiscount}from"../../functions/price";onChange("checkout",(o=>setCheckout(o,state.formId))),onChange("checkout",(o=>{(null==o?void 0:o.id)&&(state.mode=(null==o?void 0:o.live_mode)?"live":"test")})),onChangeFormState("formState",(({value:o})=>{var t,n,i,e;"draft"===o&&(null===(n=null===(t=state.checkout)||void 0===t?void 0:t.invoice)||void 0===n?void 0:n.status)&&"open"!==(null===(e=null===(i=state.checkout)||void 0===i?void 0:i.invoice)||void 0===e?void 0:e.status)&&updateFormState("LOCK")})),on("get",(o=>{if("checkout"===o){const o=getCheckout(state.formId,state.mode);(null==o?void 0:o.id)&&(state.checkout=o)}})),on("set",((o,t,n)=>{var i,e,u,d,l,r,a,c,v,s,m,p,h,f,_;if("checkout"!==o)return;if(!n||!t)return;if((null==t?void 0:t.total_amount)===(null==n?void 0:n.total_amount)&&(null==t?void 0:t.amount_due)===(null==n?void 0:n.total_amount))return;const k=getFormattedPrice({amount:t.amount_due,currency:t.currency}),C=getFormattedPrice({amount:t.total_amount,currency:t.currency}),g=(null===(e=null===(i=null==t?void 0:t.discount)||void 0===i?void 0:i.promotion)||void 0===e?void 0:e.code)!==(null===(d=null===(u=null==n?void 0:n.discount)||void 0===u?void 0:u.promotion)||void 0===d?void 0:d.code)&&(null===(r=null===(l=null==t?void 0:t.discount)||void 0===l?void 0:l.promotion)||void 0===r?void 0:r.code),F=[...(null===(c=null===(a=null==t?void 0:t.discount)||void 0===a?void 0:a.promotion)||void 0===c?void 0:c.code)===(null===(s=null===(v=null==n?void 0:n.discount)||void 0===v?void 0:v.promotion)||void 0===s?void 0:s.code)||(null===(p=null===(m=null==t?void 0:t.discount)||void 0===m?void 0:m.promotion)||void 0===p?void 0:p.code)?[]:[__("Coupon code removed.","surecart")],...g?[sprintf(
// Translators: %1$s is the coupon code, %2$s is the human readable discount.
__("Coupon code %1$s added. %2$s applied.","surecart"),null===(f=null===(h=null==t?void 0:t.discount)||void 0===h?void 0:h.promotion)||void 0===f?void 0:f.code,getHumanDiscount(null===(_=null==t?void 0:t.discount)||void 0===_?void 0:_.coupon))]:[],t.total_amount===t.amount_due?sprintf(__("Checkout updated. The amount due is %1$s.","surecart"),k):sprintf(__("Checkout updated. The total amount for the checkout is %1$s and the amount due is %1$s.","surecart"),C,k)];speak(F.join(" "))}));