"use strict";(()=>{var S=t=>{window.dataLayer=window.dataLayer||[],window.dataLayer.push({ecommerce:null}),window.dataLayer.push(t)},z=(t,r)=>{S({event:"view_cart",ecommerce:{currency:"PLN",value:r,items:t.map(v)}})};var B=t=>{S({event:"add_to_cart",ecommerce:{currency:"PLN",value:t.price*t.quantity,items:[v(t)]}})},D=t=>{S({event:"remove_from_cart",ecommerce:{currency:"PLN",value:t.price*t.quantity,items:[v(t)]}})},v=t=>({item_id:t.id,item_name:t.name,affiliation:"Google Merchandise Store",price:t.price,quantity:t.quantity});function N(){document.querySelectorAll(".sample-wrapper").forEach(r=>{r.addEventListener("click",async()=>{let n=r.querySelector('form[data-node-type="commerce-add-to-cart-form"]');if(!n)return;let e=n.getAttribute("data-commerce-sku-id"),l=n.getAttribute("data-commerce-product-id"),a=r.querySelector(".text-weight-bold")?.textContent?.trim()||"";if(!e||!l||!a){console.error("Brak danych produktu w pr\xF3bce.");return}try{let c=(await E()).find(s=>s.name.trim()===a);if(c)await x(c.id),C(r);else{T(r);let o=await(await fetch("https://hook.eu2.make.com/kr1cnajv95yw1vhqtchs7u5zvixffyuv",{method:"POST",body:JSON.stringify({productId:l,quantity:1,skuId:e})})).json(),m=o[0].fieldData,d={id:o[0].id,name:m.name,price:m.price.value/100,quantity:o[0].quantity,imageUrl:m["main-image"]?.url,weight:m.weight,length:m.length,sku:m.sku,slug:m.slug,dimensions:o[0].wymiary,realWeight:o[0].waga?Number(o[0].waga.replace(",",".")):0,categories:o[0].categories};await q(d)}}catch(i){console.error("B\u0142\u0105d przy klikni\u0119ciu pr\xF3bki:",i)}})})}function T(t){t.style.backgroundColor="green"}function C(t){t.style.backgroundColor="var(--background-color--background-product)"}var M=document.querySelector('[flip-cart="cart-button-nav-old"]'),j=document.querySelector('[flip-cart="cart-button-nav-new"]'),L=null,_=100,O=["66af5ed1a3e61b31d42650d7","66a269cf3d6464d10e4e2381"],R=!1;function A(){document.dispatchEvent(new CustomEvent("cart:updated"))}function $(t){let r=t.reduce((c,s)=>c+s.price*s.quantity,0),n=t.every(c=>c.categories.some(s=>O.includes(s))),e=t.some(c=>c.length>100),l=n&&!e,a=r>=_;return{isEligible:l&&a,total:r,remaining:Math.max(0,_-r),meetsItemRules:l,hasOnlyAllowedCategories:n,hasLongItems:e,meetsPriceRule:a}}async function E(){try{let r=await(await fetch("https://koszyk.deckline.pl/api/cart",{credentials:"include"})).json();if(!Array.isArray(r))throw new Error("Received data is not an array");return r}catch(t){return console.error("Failed to fetch cart items:",t),[]}}async function b(){try{let t=u=>u.toLocaleString("pl-PL",{minimumFractionDigits:2,maximumFractionDigits:2}),r=document.querySelector('[flip-cart="state-default"]'),n=document.querySelector('[flip-cart="state-empty"]'),e=await E(),l=e.reduce((u,h)=>u+h.price*h.quantity,0),a=document.querySelector('[flip-cart="cart-total"]');if(window.location.pathname==="/koszyk"&&l===0){alert("Tw\xF3j koszyk jest pusty. Przenosimy Ci\u0119 do sklepu."),window.location.href="/sklep";return}let i=document.querySelector('[flip-cart="cart-quantity"]');a&&(a.textContent=`${t(l)} z\u0142`),i&&(i.textContent=e.reduce((u,h)=>u+h.quantity,0).toString()),window.location.pathname==="/koszyk"&&(z(e,l),M&&j&&(M.remove(),j.remove()));let s=document.querySelector('[flip-cart="free-delivery-block-wrapper"]'),o=document.querySelector('[flip-cart="free-delivery-text"]'),m=document.querySelector('[flip-cart="free-delivery-progress"]'),{isEligible:d,remaining:g,total:w,meetsItemRules:y,meetsPriceRule:f}=$(e);if(s&&o&&m){s.classList.remove("hide"),d?(o.textContent="Darmowa dostawa!",s.classList.add("is-free")):y?f||(o.textContent=`Do darmowej dostawy brakuje ${g.toLocaleString("pl-PL",{minimumFractionDigits:2,maximumFractionDigits:2})} z\u0142`,s.classList.remove("is-free")):(o.textContent="Darmowa dostawa niedost\u0119pna",s.classList.remove("is-free"));let u=y?Math.min(w/_*100,100):0;m.style.width=`${u}%`}r&&n&&(e.length>0?(r.style.display="block",n.style.display="none"):(r.style.display="none",n.style.display="flex")),G(e),document.querySelectorAll(".sample-wrapper").forEach(u=>{let h=u.querySelector(".text-weight-bold")?.textContent?.trim();if(!h)return;e.some(I=>I.name.trim()===h.trim())?T(u):C(u)})}catch(t){console.error("Failed to update cart UI:",t)}}function U(){document.addEventListener("click",t=>{let n=t.target.closest("button[flip-cart]");if(!n)return;let e=n.getAttribute("flip-cart");if(e!=="product-quantity-plus"&&e!=="product-quantity-minus")return;t.preventDefault();let l=n.closest(".quantity-component");if(!l)return;let a=l.querySelector('input[flip-cart="product-quantity-input"], input[flip-cart="cart-quantity-input"], input[flip-cart="cart-product-quanitity"]');if(!a){console.error("Nie znalaz\u0142em inputa.");return}let i=a.min?parseInt(a.min,10):1,c=a.max?parseInt(a.max,10):1/0,s=a.step?parseInt(a.step,10):1,o=parseInt(a.value||"1",10);e==="product-quantity-plus"&&(o=Math.min(o+s,c)),e==="product-quantity-minus"&&(o=Math.max(o-s,i)),a.value=String(o),a.dispatchEvent(new Event("input",{bubbles:!0})),a.dispatchEvent(new Event("change",{bubbles:!0}))})}function W(){document.addEventListener("click",async t=>{let n=t.target.closest('[flip-cart="add-to-cart"]');if(!n)return;t.preventDefault();let e=n.querySelector('[flip-cart="add-to-cart-label"]');if(!e)return;let l=e.textContent?.trim()||"DO KOSZYKA",a=f=>{e.textContent=f},i=()=>{a(l),n.removeAttribute("aria-busy"),n.style.pointerEvents=""};a("Dodawanie\u2026"),n.setAttribute("aria-busy","true"),n.style.pointerEvents="none";let c=n.closest('[flip-cart="product-upsell"]')||n.closest('[flip-cart="product-wrapper"]');if(!c){console.error("Product element not found");return}let s=c.querySelector("form")?.getAttribute("data-commerce-product-id")||"",o=c.querySelector('form select[data-node-type="commerce-add-to-cart-option-select"]');if(o&&o.value===""){alert("Prosz\u0119 wybra\u0107 opcj\u0119 przed dodaniem produktu do koszyka."),i();return}let m=null,d=c.querySelector('form div[data-node-type="commerce-add-to-cart-pill-group"]');d&&(m=d.querySelector('form div[aria-checked="true"]'));let g=c.querySelector('input[flip-cart="product-quantity-input"]'),w=g?parseInt(g.value):1,y=o?o.value:m?.getAttribute("data-option-id")??null;y||(y=c.querySelector("form")?.getAttribute("data-commerce-sku-id")||"");try{let p=await(await fetch("https://hook.eu2.make.com/kr1cnajv95yw1vhqtchs7u5zvixffyuv",{method:"POST",body:JSON.stringify({productId:s,quantity:w,skuId:y})})).json(),u=p[0].fieldData,h={id:p[0].id,name:u.name,price:u.price.value/100,quantity:p[0].quantity,imageUrl:u["main-image"]?.url,weight:u.weight,length:u.length,sku:u.sku,slug:u.slug,dimensions:p[0].wymiary,realWeight:p[0].waga?Number(p[0].waga.replace(",",".")):0,categories:p[0].kategorie},k=h.name;if(k.startsWith("Pr\xF3bka")){let I=await E(),F=k.replace("Pr\xF3bka ","").trim();if(I.find(H=>H.name.startsWith("Pr\xF3bka")&&H.name.includes(F))){alert("Masz ju\u017C jedn\u0105 tak\u0105 pr\xF3bk\u0119 w koszyku.");return}}await q(h),a("Dodano \u2713"),setTimeout(()=>{i()},1500)}catch(f){console.error("Error getting selected item:",f),i()}})}async function q(t){try{let r=await fetch("https://koszyk.deckline.pl/api/cart",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t),credentials:"include"});if(!r.ok)throw new Error(`Failed to add item to cart: ${r.statusText}`);B(t),await b(),A(),M?.click()}catch(r){console.error("Failed to add item to cart:",r)}}async function x(t){try{let n=(await E()).find(e=>e.id===t);await fetch(`https://koszyk.deckline.pl/api/cart/${t}`,{method:"DELETE",credentials:"include"}),n&&D(n),await b(),A()}catch(r){console.error("Failed to remove item from cart:",r)}}async function J(t,r){try{await fetch(`https://koszyk.deckline.pl/api/cart/${t}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({quantity:r}),credentials:"include"}),await b(),A()}catch(n){console.error("Failed to update item quantity:",n)}}function G(t){let r=document.querySelectorAll('[flip-cart="cart-product-list"]');if(!r)return;if(!L){let e=r[0].querySelector('[flip-cart="cart-product-item"]');if(!e)return;L=e.cloneNode(!0),e.remove()}r.forEach(e=>{e.innerHTML=""});let n=e=>e.toLocaleString("pl-PL",{minimumFractionDigits:2,maximumFractionDigits:2});t.forEach(e=>{if(!L)return;let l=e.name.startsWith("Pr\xF3bka");r.forEach(a=>{let i=L?.cloneNode(!0),c=i.querySelector('[flip-cart="cart-product-image"]');c&&(c.src=e.imageUrl,c.alt=e.name);let s=i.querySelector('[flip-cart="cart-product-name"]');s&&(s.textContent=e.name,s.href=`/product/${e.slug}`);let o=i.querySelector('[flip-cart="cart-product-price"]');o&&(o.textContent=`${n(e.price*e.quantity)} z\u0142`);let m=i.querySelector('[flip-cart="cart-product-price-pcs"]');m&&(m.textContent=`${n(e.price)} z\u0142`);let d=i.querySelector('[flip-cart="cart-product-quanitity"]'),g=i.querySelector('[flip-cart="cart-product-quantity-label"]'),w=i.querySelector(".quantity-component");if(d&&g&&w){d.value=String(l?1:e.quantity);let f=l||R;d.disabled=f,f&&(d.classList.add("is-disabled"),g.classList.add("is-disabled"),w.classList.add("is-disabled")),l&&typeof window.tippy=="function"&&window.tippy(g,{content:"Dla pr\xF3bek obowi\u0105zuje limit 1 sztuki na zam\xF3wienie.",placement:"top",theme:"light-border"}),f||d.addEventListener("change",async()=>{let p=Number(d.value);p>0&&await J(e.id,p)})}let y=i.querySelector('[flip-cart="cart-product-delete"]');y&&(R?y.remove():y.addEventListener("click",async()=>{await x(e.id)})),a.appendChild(i)})})}function V(){document.addEventListener("click",async t=>{let n=t.target.closest('a[flip-cart="cart-button-cart"], a[flip-cart="cart-button-nav-new"]');if(n){t.preventDefault();try{if((await E()).reduce((a,i)=>a+i.price*i.quantity,0)<2){alert("Minimalna warto\u015B\u0107 zam\xF3wienia to 2,00 z\u0142.");return}window.location.href=n.href}catch(e){console.error("B\u0142\u0105d przy sprawdzaniu koszyka:",e),alert("Nie uda\u0142o si\u0119 sprawdzi\u0107 koszyka. Spr\xF3buj ponownie.")}}})}async function P(){await b(),U(),W(),N(),V()}var Q=async()=>{window.Webflow||(window.Webflow=[]),window.Webflow.push(async()=>{if(!window.isWebflowInitialized){window.isWebflowInitialized=!0;try{let t=new URLSearchParams(window.location.search),r=t.get("redirect_status")??"",n=t.get("payment_intent")??"",e=localStorage.getItem("payment_intent")??"";r==="succeeded"?n!==e?(localStorage.setItem("payment_intent",n),setTimeout(()=>{window.location.reload()},2e3)):await P():await P()}catch(t){console.error("B\u0142\u0105d podczas obs\u0142ugi Webflow:",t)}}})};Q();})();