import{F as K,ai as W,aN as X,r as f,m as l,N as Z,_ as aa,R as ea,a1 as ta,S as ia,e as la,h as na,l as ua}from"./vendor-DWvj1jit.js";import{j as oa,ap as sa,aq as ra,ar as da,as as va,at as ca,a8 as pa,au as ma,av as ya,aw as fa,ax as d,ay as u,L as ba,x as ga,az as _a}from"./index-8PssKxKU.js";const ha=K({__name:"add-to-cart",props:{product:{},reservedSpace:{type:Boolean}},emits:["update:lineItem"],setup(b,{emit:q}){const D=q,Q=b,S=ga(),e=X(Q,"product"),{cart:r,addToCart:w,changeItemQuantityBatched:L,addToCartLoading:g,changeItemQuantityBatchedOverflowed:_}=oa(),{t:v}=W(),{translate:M}=sa("validation_error"),I=ra(fa),{selectedConfigurationInput:C,changeCartConfiguredItemBatched:B,validateSections:k}=da(e.value.id),{trackAddItemToCart:x}=va(),{pushHistoricalEvent:N}=ca(),{themeContext:h}=pa(),o=f(!1),c=f(),R=l(()=>{if(!e.value.availabilityData?.isBuyable||!e.value.availabilityData?.isAvailable)return v("validation_error.CART_PRODUCT_UNAVAILABLE")}),z=l(()=>h.value.settings.product_quantity_control==="button"?1:0),s=l(()=>"isConfigurable"in e.value&&e.value.isConfigurable),p=l(()=>o.value||!e.value.availabilityData?.isAvailable),V=l(()=>!e.value.availabilityData?.isAvailable||_.value||g.value),$=l(()=>_.value||g.value),m=l(()=>y(r.value?.items)?.quantity||0),A=l(()=>e.value.minQuantity||z.value),j=l(()=>Math.min(e.value.availabilityData?.availableQuantity||d,na(e.value.maxQuantity)&&e.value.maxQuantity!==0?e.value.maxQuantity:d)),T=l(()=>h.value.settings.product_quantity_control==="button"?m.value||A.value:m.value),i=f(p.value?void 0:T.value);function O(a){Number.isFinite(a)?a>d?i.value=d:i.value=a:i.value=void 0}async function E(){const a=y(r.value?.items),t=a?u.Update:u.Add;if(s.value&&!k()){U(t,v("shared.catalog.product_details.product_configuration.check_your_configuration"));return}o.value=!0;try{const n=await P(a,t);if(s.value&&t===u.Add||i.value===0){o.value=!1;return}F(n,t)}finally{o.value=!1}}async function P(a,t){if(t===u.Update&&i.value===void 0)return r.value;if(t===u.Update&&a&&i.value!==void 0)return s.value?await B(a.id,i.value,C.value):await L(a.id,i.value);const n=i.value||A.value,G=s.value?C.value:void 0,J=await w(e.value.id,n,G);return x(e.value,n),N({eventType:"addToCart",productId:e.value.id}),J}function F(a,t){if(!a)return;const n=y(a.items);if(!n){ba.error(E.name,'The variable "lineItem" must be defined'),U(t,H());return}D("update:lineItem",ua.clone(n))}function U(a,t){S.error({text:v(a===u.Update?"common.messages.fail_to_change_quantity_in_cart":"common.messages.fail_add_product_to_cart",{reason:t}),duration:4e3,single:!0})}function H(){return r.value?.validationErrors?.filter(a=>a.objectId===e.value.id&&a.objectType===_a.CatalogProduct).map(M).join(" ")||""}function y(a){return s.value?I?a?.find(t=>t.id===I):void 0:a?.find(t=>t.productId===e.value.id)}function Y(a){e.value.availabilityData?.isBuyable&&!a.isValid?c.value=a.errorMessage:c.value=void 0}return Z(()=>e.value.id,()=>{i.value=p.value?void 0:T.value}),(a,t)=>(ea(),aa(ya,{mode:a.$cfg.product_quantity_control,"model-value":i.value,name:e.value.id,"count-in-cart":m.value,"min-quantity":e.value.minQuantity,"max-quantity":j.value,"pack-size":e.value.packSize,"is-active":e.value.availabilityData?.isActive,"is-available":e.value.availabilityData?.isAvailable,"is-buyable":e.value.availabilityData?.isBuyable,"is-in-stock":e.value.availabilityData?.isInStock,"available-quantity":e.value.availabilityData?.availableQuantity,message:c.value||R.value,disabled:a.$cfg.product_quantity_control==="stepper"?V.value:p.value,loading:a.$cfg.product_quantity_control==="stepper"?$.value:o.value,"show-empty-details":b.reservedSpace,"validate-on-mount":"",timeout:la(ma),"allow-zero":a.$cfg.product_quantity_control==="stepper","onUpdate:modelValue":O,"onUpdate:cartItemQuantity":E,"onUpdate:validation":Y},{default:ta(()=>[ia(a.$slots,"default")]),_:3},8,["mode","model-value","name","count-in-cart","min-quantity","max-quantity","pack-size","is-active","is-available","is-buyable","is-in-stock","available-quantity","message","disabled","loading","show-empty-details","timeout","allow-zero"]))}});export{ha as _};
//# sourceMappingURL=add-to-cart.vue_vue_type_script_setup_true_lang-DySUq1Si.js.map
