"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[856],{7938:function(e,t,r){r.d(t,{Q:function(){return n}});let n=async e=>{let t={method:e.method};return("POST"===e.method||"PUT"===e.method||"PATCH"===e.method)&&e.payload&&(t.body=JSON.stringify({data:{...e.payload}})),await fetch(e.url,{headers:{Accept:"application/json","Content-Type":"application/json"},...t}).then(e=>e.json())}},5405:function(e,t,r){r.d(t,{y:function(){return o}});var n=r(7938);let o=async e=>await (0,n.Q)({method:"POST",url:"/apiV2/product/list",payload:{ids:e}})},8664:function(e,t,r){r.d(t,{a:function(){return d},s:function(){return l}});let n=()=>new URL("https://torgsoft-online-market.com"),o=()=>n().origin,d=function(e){let t=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r="/product/".concat("object"==typeof e?e.slug:e);return t?"".concat(o()).concat(r):r},l=e=>e.sort((e,t)=>e-t).join("_")},4557:function(e,t,r){r.d(t,{_D:function(){return l},fd:function(){return n},gm:function(){return s},gx:function(){return a},q4:function(){return d},vn:function(){return o}});let n="usr_au",o="usr_crt",d="vi_prd",l="lsrch",a="otc",s="usr_ord"},3229:function(e,t,r){r.d(t,{default:function(){return w},a:function(){return g}});var n=r(7437),o=r(2265),d=r(5040),l=r(3407),a=r(9039),s=r(7519),i=r(7818),u=r(3526),c=r(8942),h=r(7938);let p=async()=>await (0,h.Q)({method:"GET",url:"/apiV2/auth/user"}),f=(0,i.default)(()=>Promise.all([r.e(231),r.e(6920),r.e(9830)]).then(r.bind(r,6920)),{loadableGenerated:{webpack:()=>[6920]},ssr:!1}),v=(0,i.default)(()=>r.e(9954).then(r.bind(r,9954)),{loadableGenerated:{webpack:()=>[9954]},ssr:!1}),P=(0,o.createContext)(void 0),g=(e,t)=>{let r=(0,o.useContext)(P);if(!r)throw Error("Missing Auth Context Provider");return(0,l.oR)(r,e,t)};var w=e=>{let{children:t,agreementLinks:r,allowedOTP:i}=e,h=(0,u.useTranslations)(),g=(0,o.useRef)();g.current||(g.current=(0,d.J)());let{userUID:w,isAuthProcess:y,isInitialized:C}=(0,l.oR)(g.current,e=>({userUID:e.userUID,isAuthProcess:e.isAuthProcess,isInitialized:e.isInitialized}),s.X),m=(0,l.oR)(g.current,c.t),{data:I,isLoading:b}=(0,a.ZP)(w?d.I+w:null,p);return(0,o.useEffect)(()=>{var e,t;window&&(null===(t=g.current)||void 0===t||null===(e=t.persist)||void 0===e||e.rehydrate())},[]),(0,o.useEffect)(()=>{C&&(m.setLoading(b,"user-loading"),b||void 0===I||m.initUser(null==I?void 0:I.data))},[I,b,C]),(0,n.jsxs)(P.Provider,{value:g.current,children:[t,(0,n.jsx)(v,{open:y,onClose:()=>m.authProcess(!1),width:"medium",title:h("pages.auth"),children:(0,n.jsx)(f,{agreementLinks:r,className:"px-2",allowedOTP:i})})]})}},5040:function(e,t,r){r.d(t,{I:function(){return l},J:function(){return a}});var n=r(7902),o=r(9291),d=r(4557);let l="/api/auth/user",a=()=>{let e={userUID:null,isInitialized:!1,isAuthenticated:!1,user:void 0,loading:!1,isAuthProcess:!1};return(0,n.M)()((0,o.tJ)((t,r)=>({...e,actions:{getOwnState:()=>r(),authProcess:e=>t({isAuthProcess:e}),initApp:()=>t({isInitialized:!0}),initUser:e=>{e?t({isAuthenticated:!0,user:e,userUID:e.username}):t({isAuthenticated:!1,user:void 0,userUID:null})},setLoading:(e,r)=>t({loading:e,loadingType:r}),setAuthState:e=>t({...e})}}),{name:d.fd,partialize:e=>({userUID:e.userUID}),skipHydration:!0,onRehydrateStorage:e=>(t,r)=>{r?console.error("An error happened during Auth hydration",r):e.actions.initApp()}}))}},856:function(e,t,r){r.d(t,{x:function(){return C}});var n=r(7437),o=r(2265),d=r(4909),l=r(3407),a=r(9039),s=r(9025),i=r(7519),u=r(3526),c=r(7818),h=r(8942),p=r(4216),f=r(8664),v=r(5405);let P=(0,c.default)(()=>r.e(9407).then(r.bind(r,9954)),{loadableGenerated:{webpack:()=>[9954]},ssr:!1}),g=(0,c.default)(()=>r.e(702).then(r.bind(r,702)),{loadableGenerated:{webpack:()=>[702]},ssr:!1}),w=(0,o.createContext)(void 0),y=o.memo(e=>{let{maxCart:t,children:r}=e,c=(0,u.useTranslations)(),{productEvent:y}=(0,d.s)(),{mapToClientProductsWithContext:C,getMainProductPriceWithContext:m}=(0,p.C)(),I=(0,o.useRef)();I.current||(I.current=(0,s.n)(t,c("context.cartMessage",{max:t})));let{isHydrated:b,selectedProducts:A,alert:x,loadCart:S}=(0,l.oR)(I.current,e=>({isHydrated:e.isCartHydrated,selectedProducts:e.selectedProducts,alert:e.alert,loadCart:e.loadCart}),i.X),k=(0,l.oR)(I.current,h.t),D=A.length>0&&null!==S,{data:H,isLoading:L}=(0,a.ZP)(D?"".concat(s.l,"/").concat((0,f.s)(A.map(e=>e.productId))):null,()=>(0,v.y)(A.map(e=>e.productId)));return(0,o.useEffect)(()=>{if(!b||(k.setLoading(L),void 0===H))return;if(null===H.data){var e;k.setLoading(!1),k.setAlert({type:"error",message:null==H?void 0:null===(e=H.error)||void 0===e?void 0:e.message});return}let t=C(H.data,A);if(t.length!==A.length){let e=A.filter(e=>!t.find(t=>t.id===e.productId)).map(e=>e.productId);k.removeProducts(e,!1)}k.addLoaded(t)},[A,b,L,H,C]),(0,o.useEffect)(()=>{var e,t,r,n;window&&(null===(t=I.current)||void 0===t||null===(e=t.persist)||void 0===e||e.onFinishHydration(()=>k.intiDelegates(y,m)),null===(n=I.current)||void 0===n||null===(r=n.persist)||void 0===r||r.rehydrate())},[]),(0,n.jsxs)(w.Provider,{value:{cartStore:I.current},children:[r,(0,n.jsx)(P,{open:!!x,onClose:()=>k.setAlert(void 0),width:"small",title:c("pages.cart"),children:(0,n.jsx)(g,{mess:x})})]})}),C=(e,t)=>{let r=(0,o.useContext)(w);if(!(null==r?void 0:r.cartStore))throw Error("Missing Cart Context Provider");return(0,l.oR)(r.cartStore,e,t)};t.default=y},9025:function(e,t,r){r.d(t,{l:function(){return l},n:function(){return a}});var n=r(7902),o=r(9291),d=r(4557);let l="/api/product/list",a=(e,t)=>{let r={isCartHydrated:!1,loadCart:null,selectedProducts:[],selectedProductsHash:new Set,productsCount:0,loadedProducts:[],isCartLoading:!1,lastEvent:null};return(0,n.M)()((0,o.tJ)((n,o)=>({...r,actions:{cartHydrated:()=>{n(e=>({isCartHydrated:!0,selectedProductsHash:new Set(e.selectedProducts.map(e=>e.productId))}))},setLoading:e=>{n({isCartLoading:e})},setAlert:e=>{n({alert:e})},setLoadCart:e=>{0!==o().selectedProducts.length&&n({loadCart:e})},clearCart:()=>{n({...r,isCartHydrated:!0})},intiDelegates:(e,t)=>{n({event:e,mainPriceResolver:t})},addProducts:(r,d)=>{let l=Array.isArray(r)?r:[r];if(!o().isCartHydrated)return;if(o().productsCount+l.length>e){n({alert:{type:"warning",message:t}});return}let a=[...o().selectedProducts],s=l.filter(e=>!a.find(t=>t.productId===e.productId)),i=[...s,...a];n({loadCart:Date.now(),selectedProducts:i,productsCount:i.length,selectedProductsHash:new Set(i.map(e=>e.productId)),lastEvent:"added",isCartLoading:!0,alert:void 0}),d&&d(s)},addLoaded:e=>{let t=o().event;"added"===o().lastEvent&&t&&t({action:"add_to_cart",product:e}).finally(),n({loadedProducts:e,isCartLoading:!1,lastEvent:null})},removeProducts:(e,t,r)=>{let d=Array.isArray(e)?e:[e],l=o().selectedProducts.filter(e=>!d.includes(e.productId)),a=o().loadedProducts.filter(e=>!d.includes(e.id)),s=o().event;if(t&&s){let e=o().loadedProducts.filter(e=>d.includes(e.id));e&&e.length>0&&s({action:"remove_from_cart",product:e}).finally()}n({selectedProducts:l,selectedProductsHash:new Set(l.map(e=>e.productId)),productsCount:l.length,loadedProducts:a}),r&&r(d)},changeQuantity:(e,t)=>{if(e.quantity<=0||0===o().selectedProducts.length||0===o().loadedProducts.length)return;let r=[...o().loadedProducts],d=[...o().selectedProducts],l=o().loadedProducts.findIndex(t=>t.id===e.productId),a=d.findIndex(t=>t.productId===e.productId);if(-1===l||-1===a)return;let s=r[l];if(!r)return;let i=o().mainPriceResolver,u=i?i(s,e):s.isDiscount?s.priceDiscount:s.price,c={...s,processedPrice:u*e.quantity,selectedQuantity:e.quantity};d[a].quantity=e.quantity,r[l]=c,n({selectedProducts:d,loadedProducts:r}),t&&t(c)},isInCart:e=>o().selectedProductsHash.has(e)}}),{name:d.vn,partialize:t=>({selectedProducts:t.selectedProducts.slice(0,e),productsCount:t.productsCount}),skipHydration:!0,onRehydrateStorage:e=>(t,r)=>{r?console.error("An error happened during Cart hydration",r):e.actions.cartHydrated()}}))}},4216:function(e,t,r){r.d(t,{C:function(){return a}});var n=r(2265),o=r(4190),d=r(3229),l=r(7519);let a=()=>{let e=(0,o.x)(e=>e.actions),t=(0,o.x)(e=>e.wholesaleMode),r=(0,d.a)(e=>e.actions),{user:a}=(0,d.a)(e=>({user:e.user}),l.X),u=(0,n.useCallback)((t,n)=>{let o=e.getOwn(),d=r.getOwnState(),l=(null==n?void 0:n.quantity)||t.minQuantity,a=i(t,l,null==d?void 0:d.user,null==o?void 0:o.wholesaleMode);return s(t,l,a)},[a,t]),c=(0,n.useCallback)((t,n)=>{let o=(null==n?void 0:n.quantity)||t.minQuantity,d=e.getOwn(),l=r.getOwnState();return i(t,o,null==l?void 0:l.user,null==d?void 0:d.wholesaleMode)},[a,t]),h=(0,n.useCallback)((e,t)=>e.map(e=>{let r=null==t?void 0:t.find(t=>t.productId===e.id);return u(e,r)}),[u]);return{mapToClientProductWithContext:u,isWholesaleUsed:(0,n.useCallback)(t=>{let n=e.getOwn(),o=r.getOwnState(),d=(null==n?void 0:n.wholesaleMode)&&!!(null==t?void 0:t.wholesalePrice)&&!!(null==t?void 0:t.wholesaleCount)&&t.wholesaleCount>0&&t.wholesalePrice>0&&t.selectedQuantity>=t.wholesaleCount;return(null==n?void 0:n.wholesaleMode)==="wholesaleRegistered"||(null==n?void 0:n.wholesaleMode)==="wholesaleSpecial"?!!(null==o?void 0:o.user)&&d:d},[a,t]),mapToClientProductsWithContext:h,getMainProductPriceWithContext:c}},s=(e,t,r)=>({...e,selectedQuantity:t,processedPrice:t*r,beforeDiscountPrice:e.isDiscount?e.price*t:null}),i=(e,t,r,n)=>{let o=e.isDiscount?e.priceDiscount:e.price;if(n&&(null==e?void 0:e.wholesalePrice)&&(null==e?void 0:e.wholesaleCount)&&e.wholesaleCount>0&&e.wholesalePrice>0&&t>=e.wholesaleCount)switch(n){case"wholesaleSpecial":(null==r?void 0:r.allowedForWholesale)&&(o=e.wholesalePrice);break;case"wholesaleRegistered":r&&(o=e.wholesalePrice);break;default:o=e.wholesalePrice}return o}}}]);