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 i,a;t.exports=(i={getConfig:()=>r.getConfig,getContext:()=>r.getContext,store:()=>r.store},a={},o.d(a,i),a)}},i={};function a(t){var e=i[t];if(void 0!==e)return e.exports;var r=i[t]={exports:{}};return o[t](r,r.exports,a),r.exports}a.d=(t,e)=>{for(var r in e)a.o(e,r)&&!a.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},a.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var n={};a.d(n,{O:()=>v,E:()=>m});var s=a(2833),c=(a(4021),a(8228));const d="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",{state:l}=(0,s.store)("woocommerce",{},{lock:d}),{state:u}=(0,s.store)("woocommerce/product-data",{},{lock:d}),m=(t,e)=>{let r,o=t;const{products:i}=(0,s.getConfig)("woocommerce");let a=null;if(e&&e.length>0){if(!i||!i[t])return null;const n=i[t].variations,s=(0,c.q)(n,e);s?.variation_id&&(o=s.variation_id,r=i?.[t]?.variations?.[s?.variation_id],a="variation")}else r=i?.[o],a=r?.type;if("object"!=typeof r||null===r)return null;const n="number"==typeof r.min?r.min:1,d="number"==typeof r.max&&r.max>=1?r.max:1/0,l=r.step||1;return{id:o,...r,min:n,max:d,step:l,type:a}},v=(t,e,r)=>{const o=l.cart?.items.find((e=>{return"variation"===e.type?!(e.id!==t||!e.variation||!r||e.variation.length!==r.length)&&(o=e,i=r,!(!Array.isArray(o.variation)||!Array.isArray(i))&&o.variation.length===i.length&&o.variation.every((({raw_attribute:t,value:e})=>i.some((r=>r.attribute===t&&(r.value.toLowerCase()===e.toLowerCase()||r.value&&""===e)))))):e.id===t;var o,i}));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 quantity(){return(0,s.getContext)().quantity||{}},get selectedAttributes(){return(0,s.getContext)().selectedAttributes||[]}},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)(),{products:o}=(0,s.getConfig)("woocommerce"),i=o?.[t].variations;i?[t,...Object.keys(i)].forEach((t=>{r.quantity[Number(t)]=e})):r.quantity={...r.quantity,[t]:e};const a=m(u.productId,r.selectedAttributes);"grouped"===a?.type?p.validateGroupedProductQuantity():p.validateQuantity(t,e)},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(a.bind(a,1401));const{selectedAttributes:t}=(0,s.getContext)(),e=u.variationId||u.productId,r=u.variationId?"variation":m(e,t)?.type;if(!r)return;if("grouped"===r)return void(yield p.batchAddToCart());const{quantity:o}=(0,s.getContext)(),i=v(e,o[e],t),{actions:n}=(0,s.store)("woocommerce",{},{lock:d});yield n.addCartItem({id:e,quantity:i,variation:t,type:r},{showCartUpdatesNotices:!1})},*handleSubmit(t){t.preventDefault();const{isFormValid:e}=g;if(e)yield p.addToCart();else{yield Promise.resolve().then(a.bind(a,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,f=n.E;export{y as getNewQuantity,f as getProductData};