import{h}from"@stencil/core";import{__}from"@wordpress/i18n";import{convertLineItemsToLineItemData}from"../../../../functions/line-items";import{createOrUpdateCheckout}from"../../../../services/session";import{state as checkoutState}from"../../../../store/checkout/index";import uiStore from"../../../../store/ui";import{updateFormState}from"../../../../store/form/mutations";const query={expand:["line_items","line_item.price","price.product","customer","customer.shipping_address","payment_intent","discount","discount.promotion","discount.coupon","shipping_address","tax_identifier"]};export class ScCartForm{constructor(){this.quantity=1,this.priceId=void 0,this.variantId=void 0,this.mode="live",this.formId=void 0,this.busy=void 0,this.error=void 0}getLineItem(){var t,e,i;const r=((null===(e=null===(t=null==checkoutState?void 0:checkoutState.checkout)||void 0===t?void 0:t.line_items)||void 0===e?void 0:e.data)||[]).find((t=>{var e,i,r;return this.variantId?(null===(e=t.variant)||void 0===e?void 0:e.id)===this.variantId&&(null===(i=t.price)||void 0===i?void 0:i.id)===this.priceId:(null===(r=t.price)||void 0===r?void 0:r.id)===this.priceId}));return!!(null==r?void 0:r.id)&&{id:null==r?void 0:r.id,price_id:null===(i=null==r?void 0:r.price)||void 0===i?void 0:i.id,quantity:null==r?void 0:r.quantity}}async addToCart(){const{price:t}=await this.form.getFormJson();try{updateFormState("FETCH"),checkoutState.checkout=await this.addOrUpdateLineItem({...t?{ad_hoc_amount:parseInt(t)||null}:{},...this.variantId?{variant_id:this.variantId||null}:{}}),updateFormState("RESOLVE"),uiStore.set("cart",{...uiStore.state.cart,open:!0})}catch(t){updateFormState("REJECT"),console.error(t),this.error=(null==t?void 0:t.message)||__("Something went wrong","surecart")}}async addOrUpdateLineItem(t={}){var e,i;let r=this.getLineItem(),o=convertLineItemsToLineItemData((null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.line_items)||[]);return await createOrUpdateCheckout({id:null===(i=null==checkoutState?void 0:checkoutState.checkout)||void 0===i?void 0:i.id,data:{live_mode:"live"===this.mode,line_items:[...(o||[]).map((e=>(this.variantId?e.price_id===this.priceId&&e.variant_id===this.variantId:e.price_id===this.priceId)?{...e,...(null==t?void 0:t.ad_hoc_amount)?{ad_hoc_amount:null==t?void 0:t.ad_hoc_amount}:{},...(null==t?void 0:t.variant_id)?{variant_id:null==t?void 0:t.variant_id}:{},quantity:(null==e?void 0:e.ad_hoc_amount)?1:(null==e?void 0:e.quantity)+1}:e)),...r?[]:[{price_id:this.priceId,variant_id:this.variantId,...(null==t?void 0:t.ad_hoc_amount)?{ad_hoc_amount:null==t?void 0:t.ad_hoc_amount}:{},quantity:1}]]},query:{...query,form_id:this.formId}})}render(){return h("sc-form",{key:"7520eac4e53e3ce0ff66a59e666cfd63084cf7d2",ref:t=>this.form=t,onScSubmit:()=>{this.addToCart()}},this.error&&h("sc-alert",{key:"4462fdd6c86f6ee8701c58706c1e72161cdcc0d6",open:!!this.error,type:"danger"},h("span",{key:"65b2e9a851c8b73208550a9d48cfe0bc0e01dc11",slot:"title"},__("Error","surecart")),this.error),h("slot",{key:"203c38679031ffa6a07f3705927fe643a52a4237"}))}static get is(){return"sc-cart-form"}static get styles(){return"sc-cart-form { display: inline-block }"}static get properties(){return{quantity:{type:"number",mutable:!0,complexType:{original:"number",resolved:"number",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The quantity"},attribute:"quantity",reflect:!1,defaultValue:"1"},priceId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The price id to add."},attribute:"price-id",reflect:!1},variantId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The variant id to add."},attribute:"variant-id",reflect:!1},mode:{type:"string",mutable:!1,complexType:{original:"'test' | 'live'",resolved:'"live" | "test"',references:{}},required:!1,optional:!1,docs:{tags:[],text:"Are we in test or live mode."},attribute:"mode",reflect:!1,defaultValue:"'live'"},formId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The form id to use for the cart."},attribute:"form-id",reflect:!0}}}static get states(){return{busy:{},error:{}}}}