import{r as i,h as t}from"./p-9add63ef.js";import{c as o}from"./p-a05701be.js";import{c as d}from"./p-de31ba85.js";import{s as e,u as n,a as s}from"./p-b28405ea.js";import"./p-8dccb656.js";import"./p-259c3935.js";import"./p-401e165e.js";import"./p-d3366af3.js";import"./p-2b28ea49.js";import"./p-dd24e53c.js";import"./p-830ab1a3.js";import"./p-ec182234.js";import"./p-3f6362a4.js";import"./p-31faf2b8.js";import"./p-610295e2.js";const r={expand:["line_items","line_item.price","line_item.component_line_items","price.product","customer","customer.shipping_address","payment_intent","discount","discount.promotion","discount.coupon","shipping_address","tax_identifier"]};const a=class{constructor(t){i(this,t);this.quantity=1;this.priceId=undefined;this.variantId=undefined;this.mode="live";this.formId=undefined;this.busy=undefined;this.error=undefined}getLineItem(){var i,t,o;const d=(((t=(i=e===null||e===void 0?void 0:e.checkout)===null||i===void 0?void 0:i.line_items)===null||t===void 0?void 0:t.data)||[]).find((i=>{var t,o,d;if(this.variantId){return((t=i.variant)===null||t===void 0?void 0:t.id)===this.variantId&&((o=i.price)===null||o===void 0?void 0:o.id)===this.priceId}return((d=i.price)===null||d===void 0?void 0:d.id)===this.priceId}));if(!(d===null||d===void 0?void 0:d.id)){return false}return{id:d===null||d===void 0?void 0:d.id,price_id:(o=d===null||d===void 0?void 0:d.price)===null||o===void 0?void 0:o.id,quantity:d===null||d===void 0?void 0:d.quantity}}async addToCart(){const{price:i}=await this.form.getFormJson();try{n("FETCH");e.checkout=await this.addOrUpdateLineItem({...!!i?{ad_hoc_amount:parseInt(i)||null}:{},...!!this.variantId?{variant_id:this.variantId||null}:{}});n("RESOLVE");s.set("cart",{...s.state.cart,...{open:true}})}catch(i){n("REJECT");console.error(i);this.error=(i===null||i===void 0?void 0:i.message)||wp.i18n.__("Something went wrong","surecart")}}async addOrUpdateLineItem(i={}){var t,n;let s=this.getLineItem();let a=o(((t=e===null||e===void 0?void 0:e.checkout)===null||t===void 0?void 0:t.line_items)||[]);return await d({id:(n=e===null||e===void 0?void 0:e.checkout)===null||n===void 0?void 0:n.id,data:{live_mode:this.mode==="live",line_items:[...(a||[]).map((t=>{const o=this.variantId?t.price_id===this.priceId&&t.variant_id===this.variantId:t.price_id===this.priceId;if(o){return{...t,...!!(i===null||i===void 0?void 0:i.ad_hoc_amount)?{ad_hoc_amount:i===null||i===void 0?void 0:i.ad_hoc_amount}:{},...!!(i===null||i===void 0?void 0:i.variant_id)?{variant_id:i===null||i===void 0?void 0:i.variant_id}:{},quantity:!(t===null||t===void 0?void 0:t.ad_hoc_amount)?(t===null||t===void 0?void 0:t.quantity)+1:1}}return t})),...!s?[{price_id:this.priceId,variant_id:this.variantId,...!!(i===null||i===void 0?void 0:i.ad_hoc_amount)?{ad_hoc_amount:i===null||i===void 0?void 0:i.ad_hoc_amount}:{},quantity:1}]:[]]},query:{...r,form_id:this.formId}})}render(){return t("sc-form",{key:"9f4a2ccb6c9a35109838c5f3ea0157a141d6e850",ref:i=>this.form=i,onScSubmit:()=>{this.addToCart()}},this.error&&t("sc-alert",{key:"e25386d06f59c3377a495f40c52006771217dbfb",open:!!this.error,type:"danger"},t("span",{key:"32fdb72ef49a9a34abc0f9b12e0c3b0e2244d142",slot:"title"},wp.i18n.__("Error","surecart")),this.error),t("slot",{key:"3cbfd139b9e1acc74ad33c30cd1e536357cfe407"}))}};a.style="sc-cart-form { display: inline-block }";export{a as sc_cart_form};
//# sourceMappingURL=p-2994f34c.entry.js.map