"use strict";(()=>{var v=document.querySelector("#cart-button"),d=document.querySelector("#added-to-cart"),h={};async function f(){try{let t=await(await fetch("https://koszyk.vsprint.pl/api/cart",{credentials:"include"})).json();if(!Array.isArray(t))throw new Error("Received data is not an array");return t}catch(a){return console.error("Failed to fetch cart items:",a),[]}}async function p(){v?.classList.remove("hide");let a=document.querySelector(".state-default"),t=document.querySelector(".state-empty"),n=document.querySelector(".state-error");try{let e=await f(),o=e.reduce((s,l)=>s+l.price*l.quantity,0),i=document.getElementById("cart-total");i&&(i.style.display="block",i.textContent=`${o.toFixed(2)} z\u0142 + VAT`);let r=document.getElementById("cart-quantity");if(r){let s=e.reduce((l,c)=>l+c.quantity,0);r.textContent=s.toString()}a&&t&&n&&(e.length>0?(t.style.display="none",n.style.display="none",a.style.display="block"):(a.style.display="none",n.style.display="none",t.style.display="flex")),await x(e)}catch(e){console.error("Failed to update cart UI:",e),a&&t&&n&&(t.style.display="none",a.style.display="none",n.style.display="flex")}}async function w(a){let t=a.closest('div[flipico-cart="product-item"]')||a.closest(".pricing19_plan")||a.closest(".home_pricing_plan");if(!t){console.error("Product element not found");return}let n=t.getAttribute("product-name"),o=t.querySelector('select[flipico-cart="product-variant"]')?.value;o&&a.setAttribute("ticket-type",n+" - "+o);let i=a.getAttribute("ticket-type");if(!i){console.error("Invalid or missing ticket type:",i),alert("Nieprawid\u0142owy typ biletu. Prosz\u0119 wybra\u0107 ponownie.");return}let r=t.querySelector('select[data-input="variant"]'),s=null;if(r&&r.getAttribute("validate")==="true"){if(r.value===""){alert("Prosz\u0119 wybra\u0107 opcj\u0119 przed dodaniem produktu do koszyka.");return}s=r.value}let l=1,c=t.querySelector('input[data-input="quantity"]');l=c&&c.value.trim()!==""?parseInt(c.value,10):1;try{await b({ticketType:i,variant:s||null,quantity:l})}catch(u){console.error("Error getting selected item:",u)}}async function b(a){let t=document.querySelector('[flipico-cart="loader"]'),n=t?.querySelector('[data-animation-type="lottie"]');try{t&&(t.classList.remove("hide"),n&&!window.cartLoaderAnimation&&(n.innerHTML="",window.cartLoaderAnimation=window.lottie.loadAnimation({container:n,renderer:"svg",loop:!0,autoplay:!1,path:n.getAttribute("data-src")||""})),window.cartLoaderAnimation&&window.cartLoaderAnimation.goToAndPlay(0,!0));let e=await fetch("https://koszyk.vsprint.pl/api/cart",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a),credentials:"include"});if(!e.ok)throw new Error(`Failed to add item to cart: ${e.statusText}`);let i=(await e.json()).find(r=>r.ticketType===a.ticketType&&(r.variant??null)===(a.variant??null));i&&(window.dataLayer.push({ecommerce:null}),window.dataLayer.push({event:"add_to_cart",ecommerce:{currency:"PLN",value:i.price*i.quantity,items:[{item_id:i.id,item_name:i.id,affiliation:"Konferencja App",price:i.price,quantity:i.quantity}]}}))}catch(e){console.error("Failed to add item to cart:",e)}finally{t&&(t.classList.add("hide"),window.cartLoaderAnimation&&window.cartLoaderAnimation.stop()),await p(),d&&(d.style.display="flex",d.style.opacity="0",d.style.transition="opacity 0.5s ease",setTimeout(()=>{d.style.opacity="1"},10),setTimeout(()=>{d.style.opacity="0",setTimeout(()=>{d.style.display="none"},500)},1500))}}var y=()=>{document.querySelectorAll('a[data-input="addtocart"]').forEach(t=>{t.dataset.listenerAdded!=="true"&&(t.addEventListener("click",async n=>{n.preventDefault();try{await w(t)}catch(e){console.error("Error adding to cart:",e)}}),t.dataset.listenerAdded="true")})};async function g(a,t=null){try{let n=t==="null"?null:t,e=await fetch(`https://koszyk.vsprint.pl/api/cart/${a}`,{method:"DELETE",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify({variant:n})});if(!e.ok)throw new Error(`Failed to remove item: ${e.statusText}`);window.dataLayer.push({ecommerce:null}),window.dataLayer.push({event:"remove_from_cart",ecommerce:{items:[{item_id:a,item_name:a,affiliation:"Konferencja App"}]}}),await p()}catch(n){console.error("Failed to remove item from cart:",n)}}async function E(a,t,n=null){try{let e=await fetch(`https://koszyk.vsprint.pl/api/cart/${a}`,{method:"PUT",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({variant:n==="null"?null:n,quantity:t,attendeeName:h[a]||[]})});if(!e.ok)throw new Error(`Failed to update item quantity: ${e.statusText}`);window.location.href.includes("koszyk")||await p()}catch(e){console.error("Failed to update item quantity:",e)}}function T(){document.addEventListener("change",async a=>{let t=a.target;if(!t.classList.contains("is-quantity"))return;let n=parseInt(t.value,10),e=t.closest(".cart-item"),o=e?.querySelector(".deletebutton")?.getAttribute("data-item-id"),i=e?.querySelector(".deletebutton")?.getAttribute("data-variant")||null;o&&n>0&&await E(o,n,i)})}async function x(a){let t=document.querySelector(".cart-list");if(!t)return;t.innerHTML="",a.forEach((e,o)=>{let i=document.createElement("div");i.className="cart-item",i.innerHTML=`