import{Host,h}from"@stencil/core";import{__}from"@wordpress/i18n";import{state as checkoutState}from"../../../../../store/checkout/index";import{updateCheckout}from"../../../../../services/session/index";import{currentFormState}from"../../../../../store/form/getters";export class ScCheckoutStockAlert{constructor(){this.stockErrors=[],this.busy=void 0,this.error=void 0}getOutOfStockLineItems(){var t,e;return((null===(e=null===(t=checkoutState.checkout)||void 0===t?void 0:t.line_items)||void 0===e?void 0:e.data)||[]).filter((t=>{var e,i,a;const o=null===(e=t.price)||void 0===e?void 0:e.product;return"out_of_stock"===(null==t?void 0:t.purchasable_status)&&((null===(i=null==t?void 0:t.variant)||void 0===i?void 0:i.id)?(null===(a=null==t?void 0:t.variant)||void 0===a?void 0:a.available_stock)<t.quantity:(null==o?void 0:o.available_stock)<t.quantity)}))}async onSubmit(){const t=this.getOutOfStockLineItems().map((t=>{var e,i,a;const o=null===(e=t.price)||void 0===e?void 0:e.product;return(null===(i=null==t?void 0:t.variant)||void 0===i?void 0:i.id)?{...t,quantity:Math.max((null===(a=null==t?void 0:t.variant)||void 0===a?void 0:a.available_stock)||0,0)}:{...t,quantity:Math.max((null==o?void 0:o.available_stock)||0,0)}}));try{this.busy=!0,checkoutState.checkout=await updateCheckout({id:checkoutState.checkout.id,data:{line_items:(t||[]).filter((t=>!!t.quantity)).map((t=>{var e,i;return{id:t.id,price_id:null===(e=t.price)||void 0===e?void 0:e.id,quantity:t.quantity,...(null===(i=null==t?void 0:t.variant)||void 0===i?void 0:i.id)?{variant:t.variant.id}:{}}}))}})}catch(t){const e=((null==t?void 0:t.additional_errors)||[]).map((t=>null==t?void 0:t.message)).filter((t=>t));this.error=`${(null==t?void 0:t.message)||__("Something went wrong.","surecart")} ${(null==e?void 0:e.length)&&` ${e.join(". ")}`}`}finally{this.busy=!1}}render(){const t=(this.getOutOfStockLineItems()||[]).map((t=>{var e,i,a;const o=null===(e=t.price)||void 0===e?void 0:e.product,l=(null===(i=null==t?void 0:t.variant)||void 0===i?void 0:i.id)?null===(a=null==t?void 0:t.variant)||void 0===a?void 0:a.available_stock:null==o?void 0:o.available_stock;return{name:null==o?void 0:o.name,image:null==t?void 0:t.image,quantity:t.quantity,available_stock:l}})),e=null==t?void 0:t.some((t=>(null==t?void 0:t.available_stock)<1));return h(Host,null,h("sc-dialog",{open:!!t.length&&"draft"===currentFormState(),noHeader:!0,onScRequestClose:t=>t.preventDefault(),class:"stock-alert"},h("sc-dashboard-module",{class:"subscription-cancel",error:this.error,style:{"--sc-dashboard-module-spacing":"1em"}},h("sc-flex",{slot:"heading","align-items":"center","justify-content":"flex-start"},h("sc-icon",{name:"alert-circle",style:{color:"var(--sc-color-primary-500"}}),__(e?"Out of Stock":"Quantity Update","surecart")),h("span",{slot:"description"},__(e?"Some items are no longer available. Your cart will be updated.":"Available quantities for these items have changed. Your cart will be updated.","surecart")),h("sc-card",{"no-padding":!0},h("sc-table",null,h("sc-table-cell",{slot:"head"},__("Description","surecart")),h("sc-table-cell",{slot:"head",style:{width:"100px",textAlign:"right"}},__("Quantity","surecart")),t.map(((e,i)=>{const a=i===t.length-1;return h("sc-table-row",{style:{"--columns":"2",...a?{border:"none"}:{}}},h("sc-table-cell",null,h("sc-flex",{justifyContent:"flex-start",alignItems:"center"},(null==e?void 0:e.image)&&h("img",{...e.image,class:"stock-alert__image"}),h("h4",null,e.name))),h("sc-table-cell",{style:{width:"100px",textAlign:"right"}},h("span",{class:"stock-alert__quantity"},h("span",null,null==e?void 0:e.quantity)," ",h("sc-icon",{name:"arrow-right"})," ",h("span",null,Math.max(null==e?void 0:e.available_stock,0)))))}))))),h("sc-button",{slot:"footer",type:"primary",loading:this.busy,onClick:()=>this.onSubmit()},__("Continue","surecart"),h("sc-icon",{name:"arrow-right",slot:"suffix"})),this.busy&&h("sc-block-ui",{spinner:!0})))}static get is(){return"sc-checkout-stock-alert"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-checkout-stock-alert.scss"]}}static get styleUrls(){return{$:["sc-checkout-stock-alert.css"]}}static get states(){return{stockErrors:{},busy:{},error:{}}}static get events(){return[{method:"scUpdateLineItem",name:"scUpdateLineItem",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Toggle line item event"},complexType:{original:"LineItemData",resolved:"LineItemData",references:{LineItemData:{location:"import",path:"src/types",id:"src/types.ts::LineItemData"}}}}]}}