import*as t from"@wordpress/interactivity";import*as e from"@woocommerce/stores/woocommerce/products";var r={d:(t,e)=>{for(var i in e)r.o(e,i)&&!r.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const i=(a={getConfig:()=>t.getConfig,getContext:()=>t.getContext,getElement:()=>t.getElement,store:()=>t.store},n={},r.d(n,a),n);var a,n;r.d({},{});const s=t=>t.replace(/^attribute_(pa_)?/,"").replace(/-/g," ").toLowerCase(),o=(t,e)=>s(t)===s(e),u=(t,e)=>{const r=t.attributes.find(t=>o(e,t.name));return r?.value},l="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",{state:c}=(0,i.store)("woocommerce/products",{},{lock:l}),d=({attributeName:t,attributeValue:e,selectedAttributes:r})=>{if(!t||!e||!Array.isArray(r))return!1;const i=r.some(e=>o(e.attribute,t))?r.length-1:r.length,{mainProductInContext:a}=c;return!!a?.variations?.length&&a.variations.some(a=>{const n=u(a,t);return(n===e||null===n)&&r.filter(r=>{const i=u(a,r.attribute);return i===r.value||!(null!==i||o(r.attribute,t)&&e!==r.value)}).length>=i})},{actions:b,state:m}=(0,i.store)("woocommerce/add-to-cart-with-options",{state:{get selectedAttributes(){const t=(0,i.getContext)();return t&&t.selectedAttributes||[]},get selectableItems(){const t=(0,i.getContext)();if(!t)return[];const{name:e,disabledAttributesAction:r,variationAttributeOptions:a}=t,{selectedAttributes:n}=m,s="hide"===r;return Array.isArray(a)?a.map((t,r)=>{const i=!d({attributeName:e,attributeValue:t.value,selectedAttributes:n}),a=n.some(r=>o(r.attribute,e)&&r.value===t.value);return{id:t.id,label:t.label,value:t.value,ariaLabel:t.ariaLabel||t.label,index:r,selected:a,disabled:i,hidden:s&&i,...void 0!==t.visual&&{visual:t.visual}}}):[]}},actions:{setAttribute(t,e){const{selectedAttributes:r}=(0,i.getContext)(),a=r.findIndex(e=>o(e.attribute,t));""!==e?a>=0?r[a]={attribute:t,value:e}:r.push({attribute:t,value:e}):a>=0&&r.splice(a,1)},removeAttribute(t){const{selectedAttributes:e}=(0,i.getContext)(),r=e.findIndex(e=>o(e.attribute,t));r>=0&&e.splice(r,1)},toggle(t){const e=(0,i.getContext)(),r=!t||t instanceof Event?e.item:t;if(!r||r.hidden||r.disabled)return;const{name:a}=e,{selectedAttributes:n}=m;n.some(t=>o(t.attribute,a)&&t.value===r.value)?(e.selectedValue="",b.setAttribute(a,"")):(e.selectedValue=r.value,b.setAttribute(a,r.value),b.autoselectAttributes({excludedAttributes:[a]}))},autoselectAttributes({includedAttributes:t=[],excludedAttributes:e=[]}={}){const r=(0,i.getContext)();if(!r||!r.autoselect)return;const{selectedAttributes:a}=m,{mainProductInContext:n}=c;if(!n)return;const o=t.map(t=>s(t)),u=e.map(t=>s(t)),l=(t=>{if(!t?.variations?.length)return{};const e={};return t.variations.forEach(t=>{t.attributes.forEach(t=>{Array.isArray(e[t.name])||(e[t.name]=[]),t.value&&!e[t.name].includes(t.value)&&e[t.name].push(t.value)})}),e})(n);Object.entries(l).forEach(([e,r])=>{const i=s(e);if(0!==o.length&&!o.includes(i))return;if(0!==u.length&&u.includes(i))return;const n=r.filter(t=>d({attributeName:e,attributeValue:t,selectedAttributes:a}));if(1===n.length){const r=n[0],a=t.find(t=>s(t)===i)||e;b.setAttribute(a,r)}})}},callbacks:{setDefaultSelectedAttribute(){const t=(0,i.getContext)();t.name&&(t.selectedValue&&b.setAttribute(t.name,t.selectedValue),b.autoselectAttributes({includedAttributes:[t.name]}))},setSelectedVariationId:()=>{const{mainProductInContext:t}=c;if(!t?.variations?.length)return;const{selectedAttributes:e}=(0,i.getContext)(),r=c.findProduct({id:t.id,selectedAttributes:e}),a=r&&r.id!==t.id?r:null,n=a?.id??null;((0,i.getContext)("woocommerce/products")||c).variationId=n},validateVariation(){b.clearErrors("variable-product");const{mainProductInContext:t}=c;if(!t?.variations?.length)return;const{selectedAttributes:e}=(0,i.getContext)(),r=c.findProduct({id:t.id,selectedAttributes:e}),a=r&&r.id!==t.id?r:null,{errorMessages:n}=(0,i.getConfig)();if(!a?.id)return void b.addError({code:"variableProductMissingAttributes",message:n?.variableProductMissingAttributes||"",group:"variable-product"});const s=c.productVariations[a.id];s&&(s.is_in_stock||b.addError({code:"variableProductOutOfStock",message:n?.variableProductOutOfStock||"",group:"variable-product"}))},watchQuantityConstraints(){const{ref:t}=(0,i.getElement)();if(!(t instanceof HTMLInputElement))return;if(t===document.activeElement)return;const{productVariationInContext:e}=c;if(!e)return;const{minimum:r,maximum:a}=e.add_to_cart,{quantity:n}=(0,i.getContext)(),s=n[e.id];let o=s;s<r?o=r:s>a&&(o=a),o===t.valueAsNumber&&o===s||b.setQuantity(e.id,o)}}},{lock:l});