JezK
Edit File: sc-checkout-stock-alert2.js
import{proxyCustomElement,HTMLElement,createEvent,h,Host}from"@stencil/core/internal/client";import{s as state}from"./mutations2.js";import{c as updateCheckout}from"./index5.js";import{c as currentFormState}from"./getters3.js";import{g as getPerBundleQuantity}from"./index6.js";import{d as defineCustomElement$c}from"./sc-alert2.js";import{d as defineCustomElement$b}from"./sc-block-ui2.js";import{d as defineCustomElement$a}from"./sc-button2.js";import{d as defineCustomElement$9}from"./sc-card2.js";import{d as defineCustomElement$8}from"./sc-dashboard-module2.js";import{d as defineCustomElement$7}from"./sc-dialog2.js";import{d as defineCustomElement$6}from"./sc-flex2.js";import{d as defineCustomElement$5}from"./sc-icon2.js";import{d as defineCustomElement$4}from"./sc-spinner2.js";import{d as defineCustomElement$3}from"./sc-table2.js";import{d as defineCustomElement$2}from"./sc-table-cell2.js";import{d as defineCustomElement$1}from"./sc-table-row2.js";const getBundleComponentVariants=(e,t=[])=>{const i={};return(t||[]).forEach((t=>{var o,n,s;if((null==t?void 0:t.bundle_line_item)!==e)return;const a=null===(n=null===(o=null==t?void 0:t.price)||void 0===o?void 0:o.product)||void 0===n?void 0:n.id,l=null===(s=null==t?void 0:t.variant)||void 0===s?void 0:s.id;a&&l&&(i[a]=l)})),i},getAvailableStock=e=>{var t,i,o;const n=null===(t=null==e?void 0:e.price)||void 0===t?void 0:t.product,s=(null===(i=null==e?void 0:e.variant)||void 0===i?void 0:i.id)?null===(o=null==e?void 0:e.variant)||void 0===o?void 0:o.available_stock:null==n?void 0:n.available_stock;return Math.max(Number(s)||0,0)},getOutOfStockLineItems=(e=[])=>(e||[]).filter((e=>"out_of_stock"===(null==e?void 0:e.purchasable_status)&&getAvailableStock(e)<e.quantity)),getBundleQuantityReductions=(e=[],t=new Map)=>{const i=new Map;return getOutOfStockLineItems(e).forEach((o=>{var n,s,a,l;if(!o.component_line_item||!o.bundle_line_item)return;const c=(e||[]).find((e=>e.id===o.bundle_line_item));if(!(null==c?void 0:c.id))return;const r=null===(s=null===(n=o.price)||void 0===n?void 0:n.product)||void 0===s?void 0:s.id;if(r&&(null===(a=t.get(c.id))||void 0===a?void 0:a.has(r)))return;const d=getPerBundleQuantity(o,c.quantity),u=Math.floor(getAvailableStock(o)/d),m=null!==(l=i.get(c.id))&&void 0!==l?l:c.quantity;i.set(c.id,Math.max(Math.min(m,u),0))})),i},buildStockAdjustedLineItems=(e=[],t=new Map)=>{const i=new Map(Array.from(t,(([e,t])=>[e,new Set(Object.keys(t))]))),o=getBundleQuantityReductions(e,i),n=new Map;return getOutOfStockLineItems(e).forEach((e=>{!e.component_line_item&&e.id&&n.set(e.id,getAvailableStock(e))})),(e||[]).filter((e=>!e.component_line_item)).map((e=>{var i,s,a,l;const c=e.id,r=!!c&&t.has(c);let d=e.quantity;return c&&o.has(c)?d=null!==(i=o.get(c))&&void 0!==i?i:d:c&&n.has(c)&&(d=null!==(s=n.get(c))&&void 0!==s?s:d),{id:c,price_id:null===(a=e.price)||void 0===a?void 0:a.id,quantity:d,...(null===(l=null==e?void 0:e.variant)||void 0===l?void 0:l.id)?{variant:e.variant.id}:{},...c&&r?{bundle_component_variants:t.get(c)}:{}}}))},buildStockAlertRows=(e=[])=>{const t=getBundleQuantityReductions(e),i=new Set,o=[];return getOutOfStockLineItems(e).forEach((n=>{var s,a,l;if(n.component_line_item&&n.bundle_line_item){const l=(e||[]).find((e=>e.id===n.bundle_line_item));if(!(null==l?void 0:l.id)||i.has(l.id))return;i.add(l.id);const c=null===(s=l.price)||void 0===s?void 0:s.product;return void o.push({name:null==c?void 0:c.name,variant:null==l?void 0:l.variant_display_options,image:null==l?void 0:l.image,from:l.quantity,to:Math.max(null!==(a=t.get(l.id))&&void 0!==a?a:l.quantity,0)})}const c=null===(l=n.price)||void 0===l?void 0:l.product;o.push({name:null==c?void 0:c.name,variant:null==n?void 0:n.variant_display_options,image:null==n?void 0:n.image,from:n.quantity,to:getAvailableStock(n)})})),o},scCheckoutStockAlertCss=":host{display:block}sc-table{height:auto}h4{display:block;margin:0;font-weight:var(--sc-font-weight-bold);font-size:var(--sc-font-size-medium)}.stock-alert{--body-spacing:var(--sc-spacing-x-large);--width:500px}.stock-alert__image{width:50px;height:50px;object-fit:cover;margin-right:10px;display:block}.stock-alert__product-info{display:flex;flex-direction:column;gap:var(--sc-spacing-xx-small)}.stock-alert__variant{color:var(--sc-color-gray-500);font-size:var(--sc-font-size-small)}.stock-alert__quantity{color:var(--sc-color-gray-500);font-weight:var(--sc-font-weight-bold);display:flex;align-items:center;justify-content:flex-end;gap:var(--sc-spacing-xx-small)}",ScCheckoutStockAlertStyle0=scCheckoutStockAlertCss,ScCheckoutStockAlert=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.scUpdateLineItem=createEvent(this,"scUpdateLineItem",7),this.stockErrors=[],this.busy=void 0,this.error=void 0}get lineItems(){var e,t;return(null===(t=null===(e=state.checkout)||void 0===e?void 0:e.line_items)||void 0===t?void 0:t.data)||[]}async onSubmit(){let e=!1;try{this.busy=!0,this.error=null;const t=this.lineItems,i=getBundleQuantityReductions(t),o=new Map;getOutOfStockLineItems(t).forEach((e=>{var n,s,a;if(!e.component_line_item||!e.bundle_line_item)return;const l=t.find((t=>t.id===e.bundle_line_item));if(!(null==l?void 0:l.id)||(null!==(n=i.get(l.id))&&void 0!==n?n:0)>=1)return;const c=null===(s=e.price)||void 0===s?void 0:s.product;if(!(null==c?void 0:c.id))return;const r=((null===(a=null==c?void 0:c.variants)||void 0===a?void 0:a.data)||[]).find((t=>{var i,o;return t.id!==(null===(i=e.variant)||void 0===i?void 0:i.id)&&(null!==(o=t.available_stock)&&void 0!==o?o:0)>0}));if(!(null==r?void 0:r.id))return;const d=o.get(l.id)||getBundleComponentVariants(l.id,t);d[c.id]=r.id,o.set(l.id,d)})),e=o.size>0;const n=buildStockAdjustedLineItems(t,o).filter((e=>!!e.quantity));state.checkout=await updateCheckout({id:state.checkout.id,data:{line_items:n}})}catch(t){if(e)this.error=wp.i18n.__("We could not automatically update an out-of-stock bundle item. Please remove it or choose a different bundle.","surecart");else{const e=((null==t?void 0:t.additional_errors)||[]).map((e=>null==e?void 0:e.message)).filter(Boolean),i=[(null==t?void 0:t.message)||wp.i18n.__("Something went wrong.","surecart")];e.length&&i.push(e.join(". ")),this.error=i.join(" ")}}finally{this.busy=!1}}render(){const e=buildStockAlertRows(this.lineItems),t=null==e?void 0:e.some((e=>(null==e?void 0:e.to)<1));return h(Host,{key:"acbef859b372585f275753310f0a0522d627b9ec"},h("sc-dialog",{key:"41f51f533d77c8f96c3c265aaaadee5ed2a80c90",open:!!e.length&&"draft"===currentFormState(),noHeader:!0,onScRequestClose:e=>e.preventDefault(),class:"stock-alert"},h("sc-dashboard-module",{key:"00ee738effe3092df20007eaed0e3ac0c4b1863b",class:"subscription-cancel",error:this.error,style:{"--sc-dashboard-module-spacing":"1em"}},h("sc-flex",{key:"54cc0304e2e6f8ce0476a05edbf1ba5222bb35a1",slot:"heading","align-items":"center","justify-content":"flex-start"},h("sc-icon",{key:"c58a11c951145aca4d0421fdc24643325b1daada",name:"alert-circle",style:{color:"var(--sc-color-primary-500"}}),t?wp.i18n.__("Out of Stock","surecart"):wp.i18n.__("Quantity Update","surecart")),h("span",{key:"9d3a9107639d100d4d815564af2714214058c190",slot:"description"},t?wp.i18n.__("Some items are no longer available. Your cart will be updated.","surecart"):wp.i18n.__("Available quantities for these items have changed. Your cart will be updated.","surecart")),h("sc-card",{key:"a01d877d96b63f948d5ae969fa414851e36127c2","no-padding":!0},h("sc-table",{key:"3b432e96899754d2bd44c298a3b30bb1e8f95d5e"},h("sc-table-cell",{key:"687fa517eef975afa99dbd9ca4b820bd8228e1ad",slot:"head"},wp.i18n.__("Description","surecart")),h("sc-table-cell",{key:"2a7f0e9bb3152acbccd57ba55e07b0eec2b3cef8",slot:"head",style:{width:"100px",textAlign:"right"}},wp.i18n.__("Quantity","surecart")),e.map(((t,i)=>{const o=i===e.length-1;return h("sc-table-row",{style:{"--columns":"2",...o?{border:"none"}:{}}},h("sc-table-cell",null,h("sc-flex",{justifyContent:"flex-start",alignItems:"center"},(null==t?void 0:t.image)&&h("img",{...t.image,class:"stock-alert__image"}),h("div",{class:"stock-alert__product-info"},h("h4",null,t.name),(null==t?void 0:t.variant)&&h("span",{class:"stock-alert__variant"},t.variant)))),h("sc-table-cell",{style:{width:"100px",textAlign:"right"}},h("span",{class:"stock-alert__quantity"},h("span",null,null==t?void 0:t.from)," ",h("sc-icon",{name:"arrow-right"})," ",h("span",null,Math.max(null==t?void 0:t.to,0)))))}))))),h("sc-button",{key:"b2fc3f985615956ba887dd9243c5b4693652cd03",slot:"footer",type:"primary",loading:this.busy,onClick:()=>this.onSubmit()},wp.i18n.__("Continue","surecart"),h("sc-icon",{key:"4a8e198f3ad2374764abc70a96516b832aeda135",name:"arrow-right",slot:"suffix"})),this.busy&&h("sc-block-ui",{key:"67747c60ab6ff8e5838d8d4c30543205cbc55248",spinner:!0})))}static get style(){return ScCheckoutStockAlertStyle0}},[1,"sc-checkout-stock-alert",{stockErrors:[32],busy:[32],error:[32]}]);function defineCustomElement(){"undefined"!=typeof customElements&&["sc-checkout-stock-alert","sc-alert","sc-block-ui","sc-button","sc-card","sc-dashboard-module","sc-dialog","sc-flex","sc-icon","sc-spinner","sc-table","sc-table-cell","sc-table-row"].forEach((e=>{switch(e){case"sc-checkout-stock-alert":customElements.get(e)||customElements.define(e,ScCheckoutStockAlert);break;case"sc-alert":customElements.get(e)||defineCustomElement$c();break;case"sc-block-ui":customElements.get(e)||defineCustomElement$b();break;case"sc-button":customElements.get(e)||defineCustomElement$a();break;case"sc-card":customElements.get(e)||defineCustomElement$9();break;case"sc-dashboard-module":customElements.get(e)||defineCustomElement$8();break;case"sc-dialog":customElements.get(e)||defineCustomElement$7();break;case"sc-flex":customElements.get(e)||defineCustomElement$6();break;case"sc-icon":customElements.get(e)||defineCustomElement$5();break;case"sc-spinner":customElements.get(e)||defineCustomElement$4();break;case"sc-table":customElements.get(e)||defineCustomElement$3();break;case"sc-table-cell":customElements.get(e)||defineCustomElement$2();break;case"sc-table-row":customElements.get(e)||defineCustomElement$1()}}))}export{ScCheckoutStockAlert as S,defineCustomElement as d};