import{r as t,h as i}from"./p-b891c2f1.js";import{_ as s}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{U as o}from"./p-aad60fb9.js";import{c as r}from"./p-eaac2001.js";import{c as e}from"./p-9a3745a6.js";import{g as n,s as a}from"./p-60f049de.js";import{u as d}from"./p-1db12eef.js";import"./p-112455b1.js";import"./p-6fd3642a.js";import"./p-1c2e2695.js";const l={expand:["line_items","line_item.price","price.product","customer","customer.shipping_address","payment_intent","discount","discount.promotion","discount.coupon","shipping_address","tax_identifier"]},c=class{constructor(i){t(this,i),this.quantity=1,this.mode="live"}getLineItem(){var t,i;const s=this.getOrder(),o=((null===(t=null==s?void 0:s.line_items)||void 0===t?void 0:t.data)||[]).find((t=>{var i;return(null===(i=t.price)||void 0===i?void 0:i.id)===this.priceId}));return(null==o?void 0:o.id)?{id:null==o?void 0:o.id,price_id:null===(i=null==o?void 0:o.price)||void 0===i?void 0:i.id,quantity:null==o?void 0:o.quantity}:{price_id:this.priceId,quantity:0}}getOrder(){return n(null==this?void 0:this.formId,this.mode)}async addToCart(){const{price:t}=await this.form.getFormJson();try{this.busy=!0;const i=await this.addOrUpdateLineItem({ad_hoc_amount:parseInt(t)||null});a(i,this.formId),d.set("cart",{...d.state.cart,open:!0})}catch(t){console.error(t),this.error=(null==t?void 0:t.message)||s("Something went wrong","surecart")}finally{this.busy=!1}}async addOrUpdateLineItem(t={ad_hoc_amount:null}){var i,s;let o=this.getLineItem(),n=r((null===(i=this.getOrder())||void 0===i?void 0:i.line_items)||[]);return o&&!(null==t?void 0:t.ad_hoc_amount)&&(o.quantity=o.quantity+1),await e({id:null===(s=this.getOrder())||void 0===s?void 0:s.id,data:{live_mode:"live"===this.mode,line_items:[...n||[],{...o,...t}]},query:{...l,form_id:this.formId}})}componentWillLoad(){o.create(this,this.state())}state(){return{busy:this.busy,error:this.error,order:this.getOrder()}}render(){return i("sc-form",{ref:t=>this.form=t,onScSubmit:()=>{console.log("submit"),this.addToCart()}},this.error&&i("sc-alert",{open:!!this.error,type:"danger"},i("span",{slot:"title"},s("Error","surecart")),this.error),i(o.Provider,{state:this.state()},i("slot",null)))}};c.style="sc-cart-form { display: inline-block }";export{c as sc_cart_form}