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,u;"draft"===o&&(null===(n=null===(t=state.checkout)||void 0===t?void 0:t.invoice)||void 0===n?void 0:n.status)&&"open"!==(null===(u=null===(i=state.checkout)||void 0===i?void 0:i.invoice)||void 0===u?void 0:u.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,u,e,d,l,r,c,a,v,m,s,p,f,h,_;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===(u=null===(i=null==t?void 0:t.discount)||void 0===i?void 0:i.promotion)||void 0===u?void 0:u.code)!==(null===(d=null===(e=null==n?void 0:n.discount)||void 0===e?void 0:e.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===(a=null===(c=null==t?void 0:t.discount)||void 0===c?void 0:c.promotion)||void 0===a?void 0:a.code)===(null===(m=null===(v=null==n?void 0:n.discount)||void 0===v?void 0:v.promotion)||void 0===m?void 0:m.code)||(null===(p=null===(s=null==t?void 0:t.discount)||void 0===s?void 0:s.promotion)||void 0===p?void 0:p.code)?[]:[__("Coupon code removed.","sc-coupon-form")],...g?[sprintf(
// Translators: %1$s is the coupon code, %2$s is the human readable discount.
__("Coupon code %1$s added. %2$s applied.","sc-coupon-form"),null===(h=null===(f=null==t?void 0:t.discount)||void 0===f?void 0:f.promotion)||void 0===h?void 0:h.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(" "))}));