import{F as K,aM as W,ai as X,r as f,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 ua}from"./vendor-BmRNukEL.js";import{x as oa,k as sa,aq as ra,ar as da,as as va,at as ca,au as pa,a6 as ma,av as d,aw as ya,ax as fa,ay as ba,az as u,L as ga,aA as _a}from"./index-CfcNWeCf.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,M=oa(),e=W(Q,"product"),{cart:r,addToCart:S,changeItemQuantityBatched:w,addToCartLoading:g,changeItemQuantityBatchedOverflowed:_}=sa(),{t:v}=X(),{translate:L}=ra("validation_error"),I=da(ba),{selectedConfigurationInput:C,changeCartConfiguredItemBatched:k,validateSections:B}=va(e.value.id),{trackAddItemToCart:x}=ca(),{pushHistoricalEvent:N}=pa(),{themeContext:h}=ma(),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),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=f(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=y(r.value?.items),t=a?u.Update:u.Add;if(s.value&&!B()){U(t,v("shared.catalog.product_details.product_configuration.check_your_configuration"));return}o.value=!0;try{const n=await j(a,t);if(s.value&&t===u.Add||i.value===0){o.value=!1;return}F(n,t)}finally{o.value=!1}}async function j(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 k(a.id,i.value,C.value):await w(a.id,i.value);const n=i.value||A.value,G=s.value?C.value:void 0,J=await S(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){ga.error(E.name,'The variable "lineItem" must be defined'),U(t,H());return}D("update:lineItem",ua.clone(n))}function U(a,t){M.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(L).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 aa(()=>e.value.id,()=>{i.value=p.value?void 0:T.value}),(a,t)=>(ta(),ea(fa,{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"?V.value:p.value,loading:a.$cfg.product_quantity_control==="stepper"?$.value:o.value,"show-empty-details":b.reservedSpace,"validate-on-mount":"",timeout:na(ya),"allow-zero":a.$cfg.product_quantity_control==="stepper","onUpdate:modelValue":P,"onUpdate:cartItemQuantity":E,"onUpdate:validation":Y},{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-mgqFFYIu.js.map
