import{V as e,Wt as t,_ as n,bt as r,dt as i,f as a,i as o,l as s,lt as c,p as l,qt as u,rt as d,u as f,z as p}from"./runtime-core.esm-bundler.v79eHMSw.js";import{c as m,d as h,r as g}from"./runtime-dom.esm-bundler.BbL3gL8L.js";var _={class:`smb2b-matrix`},v={class:`smb2b-matrix__head`},y={class:`smb2b-matrix__hint`},b={key:0,class:`smb2b-matrix__rule`},x={class:`smb2b-matrix__rule-label`},S={class:`smb2b-matrix__scroll`},ee={class:`smb2b-matrix__table`},C={class:`smb2b-matrix__corner`},w={key:0,class:`smb2b-matrix__col smb2b-matrix__col--total`},te={class:`smb2b-matrix__row`},ne=[`onUpdate:modelValue`,`aria-label`,`title`,`onKeydown`,`onPaste`,`onInput`],re=[`title`],ie={key:2,class:`smb2b-matrix__price`},ae={key:1,class:`smb2b-matrix__void`},oe={key:0,class:`smb2b-matrix__cell smb2b-matrix__rowtotal`},se={key:0},ce={key:0},le={class:`smb2b-matrix__totals`},ue={class:`smb2b-matrix__row`},T={key:0},E={class:`smb2b-matrix__cell smb2b-matrix__grandtotal`},D=[`href`],O={class:`smb2b-matrix__foot`},k={class:`smb2b-matrix__total`},A={class:`smb2b-matrix__foot-actions`},j=[`disabled`],M={__name:`Matrix`,props:{config:{type:Object,required:!0}},setup(g){let M=g.config,N=M.grid,P=M.currency,F=M.i18n,I=N.cols.length>1||N.cols[0]&&N.cols[0].label!==``,L=(()=>{let e=M.rule;if(!e)return null;let t=[];return e.min!=null&&t.push(String(F.ruleMin).replace(`%d`,e.min)),e.step!=null&&t.push(String(F.ruleStep).replace(`%d`,e.step)),e.max!=null&&t.push(String(F.ruleMax).replace(`%d`,e.max)),t.length?t:null})(),R=c(N.rows.map(()=>N.cols.map(()=>``))),z=c({}),B=i(!1),V=c({msg:``,type:``}),H=(e,t)=>N.cells[e]&&N.cells[e][t]?N.cells[e][t]:null,U=(e,t)=>{let n=parseInt(String(R[e][t]).trim(),10);return Number.isFinite(n)&&n>0?n:0},W=s(()=>{let e=0;return N.rows.forEach((t,n)=>N.cols.forEach((t,r)=>{H(n,r)&&H(n,r).in_stock&&(e+=U(n,r))})),e}),de=s(()=>{let e=0;return N.rows.forEach((t,n)=>N.cols.forEach((t,r)=>{let i=H(n,r);i&&i.in_stock&&(e+=U(n,r)*i.price)})),e}),G=I,K=e=>{let t=0;return N.cols.forEach((n,r)=>{H(e,r)&&H(e,r).in_stock&&(t+=U(e,r))}),t},q=e=>{let t=0;return N.rows.forEach((n,r)=>{H(r,e)&&H(r,e).in_stock&&(t+=U(r,e))}),t};function J(e){let[t,n]=Math.max(0,Math.round((e+2**-52)*100)/100).toFixed(P.decimals).split(`.`);t=t.replace(/\B(?=(\d{3})+(?!\d))/g,P.thousandSep);let r=n?t+P.decimalSep+n:t;switch(P.position){case`right`:return r+P.symbol;case`left_space`:return P.symbol+` `+r;case`right_space`:return r+` `+P.symbol;default:return P.symbol+r}}let Y=(e,t,n)=>String(e===1?t:n).replace(`%d`,e),X=(e,t)=>{let n=H(e,t);return n&&z[n.id]||``},Z=(e,t)=>{let n=H(e,t);n&&delete z[n.id]},fe=(e,t)=>({"is-filled":U(e,t)>0,"is-failed":!!X(e,t),"is-oos":!!(H(e,t)&&!H(e,t).in_stock)}),Q=new Map,pe=(e,t,n)=>{n?Q.set(e+`:`+t,n):Q.delete(e+`:`+t)};function me(e,t,n,r){let i=e+n,a=t+r;for(;i>=0&&i<N.rows.length&&a>=0&&a<N.cols.length;){let e=Q.get(i+`:`+a);if(e){e.focus(),e.select();return}i+=n,a+=r}}function he(e,t,n){let r={ArrowUp:[-1,0],ArrowDown:[1,0],ArrowLeft:[0,-1],ArrowRight:[0,1],Enter:[1,0]}[e.key];r&&(e.key===`ArrowLeft`&&e.target.selectionStart>0||e.key===`ArrowRight`&&e.target.selectionStart<e.target.value.length||(e.preventDefault(),me(t,n,r[0],r[1])))}function ge(e,t,n){let r=((e.clipboardData||window.clipboardData).getData(`text`)||``).replace(/\r/g,``).split(`
`).filter((e,t,n)=>e!==``||t<n.length-1);if(r.length===1&&r[0].indexOf(`	`)===-1){let e=parseInt(r[0].trim(),10);R[t][n]=Number.isFinite(e)&&e>=0?String(e):R[t][n],Z(t,n);return}r.forEach((e,r)=>{e.split(`	`).forEach((e,i)=>{let a=t+r,o=n+i;if(a>=N.rows.length||o>=N.cols.length)return;let s=H(a,o);if(!s||!s.in_stock)return;let c=parseInt(String(e).trim(),10);Number.isFinite(c)&&c>=0&&(R[a][o]=c===0?``:String(c),Z(a,o))})})}function $(){N.rows.forEach((e,t)=>N.cols.forEach((e,n)=>{R[t][n]=``})),Object.keys(z).forEach(e=>delete z[e]),V.msg=``}async function _e(){let e=[];if(N.rows.forEach((t,n)=>N.cols.forEach((t,r)=>{let i=H(n,r),a=U(n,r);i&&i.in_stock&&a>0&&e.push({variation_id:i.id,qty:a})})),!(!e.length||B.value)){B.value=!0,V.msg=``,Object.keys(z).forEach(e=>delete z[e]);try{let t=await fetch(M.apiUrl+`/storefront/matrix/add`,{method:`POST`,headers:{"Content-Type":`application/json`,"X-WP-Nonce":M.nonce},credentials:`same-origin`,body:JSON.stringify({lines:e})}),n=await t.json().catch(()=>({}));if(!t.ok)throw Error(n.message||F.error);let r=n.data&&n.data.results||[],i=0;r.forEach(e=>{if(e.status===`added`){i++;return}z[e.variation_id]=e.reason||F.error});let a=new Set(e.map(e=>e.variation_id));N.rows.forEach((e,t)=>N.cols.forEach((e,n)=>{let r=H(t,n);r&&a.has(r.id)&&!z[r.id]&&(R[t][n]=``)}));let o=r.length-i;V.type=o?`warn`:`success`,V.msg=o?Y(i,F.addedOne,F.addedMany)+` `+Y(o,F.failedOne,F.failedMany):Y(i,F.addedOne,F.addedMany)}catch(e){V.type=`error`,V.msg=e.message}finally{B.value=!1}}}return(i,s)=>(p(),l(`div`,_,[f(`div`,v,[f(`strong`,null,u(r(F).title),1),f(`span`,y,u(r(F).hint),1),r(L)?(p(),l(`span`,b,[f(`span`,x,u(r(F).ruleLabel),1),(p(!0),l(o,null,e(r(L),(e,t)=>(p(),l(`span`,{key:t,class:`smb2b-matrix__rule-chip`},u(e),1))),128))])):a(``,!0)]),f(`div`,S,[f(`table`,ee,[f(`thead`,null,[f(`tr`,null,[f(`th`,C,[n(u(r(N).rowLabel),1),r(I)?(p(),l(o,{key:0},[n(` \\ `+u(r(N).colLabel),1)],64)):a(``,!0)]),(p(!0),l(o,null,e(r(N).cols,e=>(p(),l(`th`,{key:e.key,class:`smb2b-matrix__col`},u(e.label||r(F).quantity),1))),128)),r(G)?(p(),l(`th`,w,u(r(F).total),1)):a(``,!0)])]),f(`tbody`,null,[(p(!0),l(o,null,e(r(N).rows,(n,i)=>(p(),l(`tr`,{key:n.key},[f(`th`,te,u(n.label),1),(p(!0),l(o,null,e(r(N).cols,(e,s)=>(p(),l(`td`,{key:e.key,class:t([`smb2b-matrix__cell`,fe(i,s)])},[H(i,s)?(p(),l(o,{key:0},[H(i,s).in_stock?d((p(),l(`input`,{key:0,ref_for:!0,ref:e=>pe(i,s,e),"onUpdate:modelValue":e=>R[i][s]=e,type:`text`,inputmode:`numeric`,autocomplete:`off`,class:`smb2b-matrix__qty`,"aria-label":n.label+(e.label?` / `+e.label:``),title:X(i,s)||``,onKeydown:e=>he(e,i,s),onPaste:h(e=>ge(e,i,s),[`prevent`]),onInput:e=>Z(i,s)},null,40,ne)),[[m,R[i][s]]]):(p(),l(`span`,{key:1,class:`smb2b-matrix__oos`,title:r(F).outOfStock},`—`,8,re)),H(i,s).in_stock?(p(),l(`span`,ie,u(J(H(i,s).price)),1)):a(``,!0)],64)):(p(),l(`span`,ae))],2))),128)),r(G)?(p(),l(`td`,oe,[K(i)?(p(),l(`span`,se,u(K(i)),1)):a(``,!0)])):a(``,!0)]))),128))]),r(G)?(p(),l(`tfoot`,ce,[f(`tr`,le,[f(`th`,ue,u(r(F).total),1),(p(!0),l(o,null,e(r(N).cols,(e,t)=>(p(),l(`td`,{key:e.key,class:`smb2b-matrix__cell smb2b-matrix__coltotal`},[q(t)?(p(),l(`span`,T,u(q(t)),1)):a(``,!0)]))),128)),f(`td`,E,u(W.value||``),1)])])):a(``,!0)])]),V.msg?(p(),l(`div`,{key:0,class:t([`smb2b-matrix__notice`,`is-`+V.type]),role:`status`},[n(u(V.msg)+` `,1),V.type===`success`&&r(M).cartUrl?(p(),l(`a`,{key:0,href:r(M).cartUrl},u(r(F).viewCart),9,D)):a(``,!0)],2)):a(``,!0),f(`div`,O,[f(`span`,k,[n(u(Y(W.value,r(F).unitOne,r(F).unitMany))+` `,1),W.value?(p(),l(o,{key:0},[s[0]||=n(` · `,-1),f(`strong`,null,u(J(de.value)),1)],64)):a(``,!0)]),f(`span`,A,[W.value?(p(),l(`button`,{key:0,type:`button`,class:`smb2b-matrix__clear`,onClick:$},u(r(F).clear),1)):a(``,!0),f(`button`,{type:`button`,class:`smb2b-matrix__submit`,disabled:!W.value||B.value,onClick:_e},u(B.value?r(F).adding:r(F).addToCart),9,j)])])]))}},N=document.getElementById(`smb2b-matrix`);N&&window.smb2bMatrix!==void 0&&g(M,{config:window.smb2bMatrix}).mount(N);