import{F as J,aK as W,ag as X,r as y,m as l,h as Z,N as aa,_ as ea,R as ta,a1 as ia,S as la,e as na,l as oa}from"./vendor-CB-jpi5M.js";import{v as ua,i as sa,an as ra,ao as da,ap as va,aq as ca,ar as pa,a2 as ma,as as d,at as fa,au as ya,av as ba,aw as o,L as ga,ax as _a}from"./index-Bjc9_X6l.js";const ha=J({__name:"add-to-cart",props:{product:{},reservedSpace:{type:Boolean}},emits:["update:lineItem"],setup(b,{emit:q}){const D=q,Q=b,S=ua(),e=W(Q,"product"),{cart:r,addToCart:w,changeItemQuantityBatched:L,addToCartLoading:g,changeItemQuantityBatchedOverflowed:_}=sa(),{t:v}=X(),{translate:M}=ra("validation_error"),I=da(ba),{selectedConfigurationInput:C,changeCartConfiguredItemBatched:B,validateSections:k}=va(e.value.id),{trackAddItemToCart:x}=ca(),{pushHistoricalEvent:N}=pa(),{themeContext:h}=ma(),u=y(!1),c=y(),R=l(()=>{if(!e.value.availabilityData?.isBuyable||!e.value.availabilityData?.isAvailable)return v("validation_error.CART_PRODUCT_UNAVAILABLE")}),V=l(()=>h.value.settings.product_quantity_control==="button"?1:0),s=l(()=>"isConfigurable"in e.value&&e.value.isConfigurable),p=l(()=>u.value||!e.value.availabilityData?.isAvailable),$=l(()=>!e.value.availabilityData?.isAvailable||_.value||g.value),z=l(()=>_.value||g.value),m=l(()=>f(r.value?.items)?.quantity||0),A=l(()=>e.value.minQuantity||V.value),O=l(()=>Math.min(e.value.availabilityData?.availableQuantity||d,Z(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=y(p.value?void 0:T.value);function P(a){Number.isFinite(a)?a>d?i.value=d:i.value=a:i.value=void 0}async function E(){const a=f(r.value?.items),t=a?o.Update:o.Add;if(s.value&&!k()){U(t,v("shared.catalog.product_details.product_configuration.check_your_configuration"));return}u.value=!0;try{const n=await j(a,t);if(s.value&&t===o.Add||i.value===0){u.value=!1;return}F(n,t)}finally{u.value=!1}}async function j(a,t){if(t===o.Update&&i.value===void 0)return r.value;if(t===o.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,Y=s.value?C.value:void 0,G=await w(e.value.id,n,Y);return x(e.value,n),N({eventType:"addToCart",productId:e.value.id}),G}function F(a,t){if(!a)return;const n=f(a.items);if(!n){ga.error(E.name,'The variable "lineItem" must be defined'),U(t,H());return}D("update:lineItem",oa.clone(n))}function U(a,t){S.error({text:v(a===o.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 f(a){return s.value?I?a?.find(t=>t.id===I):void 0:a?.find(t=>t.productId===e.value.id)}function K(a){e.value.availabilityData?.isBuyable&&!a.isValid?c.value=a.errorMessage:c.value=void 0}return aa(()=>e.value.id,()=>{i.value=p.value?void 0:T.value}),(a,t)=>(ta(),ea(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":O.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"?$.value:p.value,loading:a.$cfg.product_quantity_control==="stepper"?z.value:u.value,"show-empty-details":b.reservedSpace,"validate-on-mount":"",timeout:na(fa),"allow-zero":a.$cfg.product_quantity_control==="stepper","onUpdate:modelValue":P,"onUpdate:cartItemQuantity":E,"onUpdate:validation":K},{default:ia(()=>[la(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-CCgaMN-U.js.map
