import*as t from"@woocommerce/stores/woocommerce/cart";import*as e from"@woocommerce/stores/woocommerce/product-data";import*as r from"@wordpress/interactivity";var o={8228:(t,e,r)=>{r.d(e,{q:()=>o});const o=(t,e)=>{if(!t||!Object.keys(t).length||!Array.isArray(e)||0===e.length)return null;const r=Object.entries(t).find((([t,r])=>Object.entries(r.attributes).every((([t,r])=>e.some((e=>!(e.attribute!==t)&&(e.value===r||e.value&&""===r)))))));return r?{...r[1],variation_id:Number(r[0])}:null}},7908:t=>{t.exports=import("@woocommerce/stores/store-notices")},1401:e=>{e.exports=t},4021:(t,e,r)=>{var o;t.exports=(o={},r.d(o,{}),o)},2833:(t,e,o)=>{var a,i;t.exports=(a={getConfig:()=>r.getConfig,getContext:()=>r.getContext,store:()=>r.store},i={},o.d(i,a),i)}},a={};function i(t){var e=a[t];if(void 0!==e)return e.exports;var r=a[t]={exports:{}};return o[t](r,r.exports,i),r.exports}i.d=(t,e)=>{for(var r in e)i.o(e,r)&&!i.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var n={};i.d(n,{O:()=>v,E:()=>m});var s=i(2833),c=(i(4021),i(8228));const d="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",{state:u}=(0,s.store)("woocommerce",{},{lock:d}),{state:l}=(0,s.store)("woocommerce/product-data",{},{lock:d}),m=(t,e)=>{const{products:r}=(0,s.getConfig)("woocommerce");if(!r||!r[t])return null;let o={id:t,...r[t]};if("variable"===o.type&&e&&e.length>0){const t=(0,c.q)(o.variations,e);t&&(o={...t,id:t.variation_id,type:"variation"})}const a="number"==typeof o.min?o.min:1,i="number"==typeof o.max?Math.max(o.max,0):1/0,n="number"==typeof o.step&&o.step>0?o.step:1;return{...o,min:a,max:i,step:n}},v=(t,e,r)=>{const o=u.cart?.items.find((e=>{return"variation"===e.type?!(e.id!==t||!e.variation||!r||e.variation.length!==r.length)&&(o=e,a=r,!(!Array.isArray(o.variation)||!Array.isArray(a))&&o.variation.length===a.length&&o.variation.every((({raw_attribute:t,value:e})=>a.some((r=>r.attribute===t&&(r.value.toLowerCase()===e.toLowerCase()||r.value&&""===e)))))):e.id===t;var o,a}));return(o?.quantity||0)+e},{actions:p,state:g}=(0,s.store)("woocommerce/add-to-cart-with-options",{state:{noticeIds:[],get validationErrors(){const t=(0,s.getContext)();return t&&t.validationErrors?t.validationErrors:[]},get isFormValid(){return 0===g.validationErrors.length},get allowsAddingToCart(){var t;const{productData:e}=g;return null===(t=e?.is_in_stock)||void 0===t||t},get quantity(){return(0,s.getContext)().quantity},get selectedAttributes(){return(0,s.getContext)().selectedAttributes||[]},get productData(){const{selectedAttributes:t}=(0,s.getContext)();return m(l.productId,t)}},actions:{validateQuantity(t,e){if(p.clearErrors("invalid-quantities"),"number"!=typeof e)return;const{selectedAttributes:r}=(0,s.getContext)(),o=m(t,r);if(0===e||o&&(e<o.min||e>o.max)){const{errorMessages:t}=(0,s.getConfig)();p.addError({code:"invalidQuantities",message:t?.invalidQuantities||"",group:"invalid-quantities"})}},setQuantity(t,e){const r=(0,s.getContext)(),o=(0,s.getContext)("woocommerce/add-to-cart-with-options-quantity-selector"),a=o?.inputElement,{products:i}=(0,s.getConfig)("woocommerce"),n=i?.[t].variations,c=Number.isNaN(a?.valueAsNumber);n?[t,...Object.keys(n)].forEach((t=>{c&&(r.quantity[Number(t)]=null),r.quantity[Number(t)]=e})):(c&&(r.quantity={...r.quantity,[t]:null}),r.quantity={...r.quantity,[t]:e}),"grouped"===g.productData?.type?p.validateGroupedProductQuantity():p.validateQuantity(t,e),a&&(t=>{const e=new Event("change",{bubbles:!0});t.dispatchEvent(e)})(a)},addError:t=>{const{validationErrors:e}=g;return e.push(t),t.code},clearErrors:t=>{const{validationErrors:e}=g;if(t){const r=e.filter((e=>e.group!==t));e.splice(0,e.length,...r)}else e.length=0},*addToCart(){yield Promise.resolve().then(i.bind(i,1401));const{selectedAttributes:t}=(0,s.getContext)(),e=l.variationId||l.productId,r=l.variationId?"variation":m(e,t)?.type;if(!r)return;if("grouped"===r)return void(yield p.batchAddToCart());const{quantity:o}=(0,s.getContext)(),a=v(e,o[e],t),{actions:n}=(0,s.store)("woocommerce",{},{lock:d});yield n.addCartItem({id:e,quantity:a,variation:t,type:r},{showCartUpdatesNotices:!1})},*handleSubmit(t){t.preventDefault();const{isFormValid:e}=g;if(e)yield p.addToCart();else{yield Promise.resolve().then(i.bind(i,7908));const{actions:t}=(0,s.store)("woocommerce/store-notices",{},{lock:d}),{noticeIds:e,validationErrors:r}=g;e.forEach((e=>{t.removeNotice(e)})),e.splice(0,e.length);const o=r.map((e=>t.addNotice({notice:e.message,type:"error",dismissible:!0})));e.push(...o)}}}},{lock:d});var y=n.O,b=n.E;export{y as getNewQuantity,b as getProductData};