import{s as $}from"./store-api-Cl_W-5C2.js";import"./api-client-QIUkDUfA.js";let b=!1;window.basketCache={summary:null,lastUpdated:null,isLoading:!1};function m(){return console.log("loadBasketSummary called, isLoading:",window.basketCache.isLoading),window.basketCache.isLoading?(console.log("Skipping load - already loading"),Promise.resolve()):(console.log("Starting API request..."),window.basketCache.isLoading=!0,$.getBasketSummary().then(e=>{var t;if(e.success&&e.summary&&(window.basketCache.summary=e.summary,window.basketCache.lastUpdated=Date.now(),x(),!b&&window.procpFireCustomEvent))try{const n=e.summary;window.procpFireCustomEvent("BasketViewed",{items_count:Array.isArray(n.items)?n.items.length:0,total:Number(n.total||0),currency:typeof n.currency=="string"?n.currency:((t=n.currency)==null?void 0:t.code)||window.procpDefaultCurrency||"USD"}),b=!0}catch(n){console.warn("[tracking] BasketViewed emit failed",n)}}).catch(e=>{throw console.error("Basket summary load error:",e),e}).finally(()=>{window.basketCache.isLoading=!1}))}function x(){const e=document.getElementById("basketCount");!e||!window.basketCache.summary||(window.basketCache.summary.totalItems>0?(e.textContent=window.basketCache.summary.totalItems,e.style.display="inline-block"):e.style.display="none")}function g(){console.log("updateBasketCount called, cache summary:",window.basketCache.summary),window.basketCache.summary?(console.log("Using cached data for display"),x()):(console.log("No cached data, loading from API..."),m())}function B(){window.basketCache.summary=null,window.basketCache.lastUpdated=null,m()}let k;function C(){const e=document.getElementById("basketPopup");e&&(e.style.display="block",_())}function f(){const e=document.getElementById("basketPopup");e&&(e.style.display="none")}function _(){var t;const e=document.getElementById("basketPopupContent");if(e){if(window.basketCache.summary){v(window.basketCache.summary);return}e.innerHTML=`
                <div class="text-center py-3">
                    <div class="spinner-border spinner-border-sm text-primary" role="status">
                        <span class="visually-hidden">Yükleniyor...</span>
                    </div>
                    <div class="mt-2 text-muted">${((t=window.translations)==null?void 0:t.store_basket_loading)||"Sepet yükleniyor..."}</div>
                </div>
    `,m().then(()=>{window.basketCache.summary?v(window.basketCache.summary):e.innerHTML=`
                <div class="text-center py-3 text-muted">
                    <i class="fas fa-shopping-cart fa-2x mb-2"></i>
                    <div>Sepetiniz boş</div>
                </div>
            `}).catch(n=>{console.error("Basket popup load error:",n),e.innerHTML=`
            <div class="text-center py-3 text-danger">
                <i class="fas fa-exclamation-triangle fa-2x mb-2"></i>
                <div>Sepet yüklenirken hata oluştu</div>
            </div>
        `})}}function E(e){if(!e||e.length===0)return"";let t="";const n={};return e.forEach(a=>{if(!a||!a.option_id){console.warn("Invalid config option:",a);return}const i=a.option_id;n[i]||(n[i]=[]),n[i].push(a)}),Object.keys(n).forEach(a=>{const i=n[a],r=i[0],d=r.name&&r.name.includes("(x")&&r.quantity>1,s=i.length>1;if(d){const l=w(r.name);t+=`<div class="text-muted small">+ <strong>${l}</strong>: ${r.quantity}</div>`}else if(s){const l=i.map(o=>o&&o.name?y(o.name):null).filter(o=>o);if(l.length>0){const o=w(r.name);t+=`<div class="text-muted small">+ <strong>${o}</strong>: ${l.join(", ")}</div>`}}else{const l=y(r.name);l&&(t+=`<div class="text-muted small">+ <strong>${w(r.name)}</strong>: ${l}</div>`)}}),t}function w(e){return!e||typeof e!="string"?"Option":e.includes(" (x")?e.split(" (x")[0]:e.includes(" - ")?e.split(" - ")[0]:e}function y(e){return!e||typeof e!="string"||e.includes(" (x")?null:e.includes(" - ")?e.split(" - ")[1]:e}function v(e){var a,i,r,d;const t=document.getElementById("basketPopupContent");if(!t)return;let n="";e.items&&e.items.length>0?(n+='<div class="basket-items">',e.items.forEach((s,l)=>{var o,c;n+=`
                <div class="d-flex justify-content-between align-items-start mb-2 pb-2 border-bottom">
                    <div class="flex-grow-1">
                        <div class="fw-medium small">${s.name}</div>
                        <div class="text-muted small">${s.period} ${s.period_type||((o=window.translations)==null?void 0:o.store_basket_period_month)||"ay"}</div>
                        ${s.configurableOptions&&s.configurableOptions.length>0?E(s.configurableOptions):""}
                        ${s.connectedDomain?`<div class="text-muted small">+ ${((c=window.translations)==null?void 0:c.store_basket_domain)||"Domain"}: ${s.connectedDomain.name||s.connectedDomain}</div>`:""}
                        ${s.customFieldsDisplay&&s.customFieldsDisplay.length||s.customFields&&Object.keys(s.customFields).length>0?F(s.customFieldsDisplay||s.customFields):""}
                        ${s.type==="domain"&&s.nameservers&&s.nameservers.length>0?L(s.nameservers):""}
                    </div>
                    <div class="text-end">
                        <div class="fw-medium small">${p(s.price,e.currency)}</div>
                    </div>
                </div>
            `}),n+="</div>",n+=`
            <div class="mt-3 pt-2 border-top">
                <div class="d-flex justify-content-between align-items-center mb-1">
                    <span>${((a=window.translations)==null?void 0:a.store_basket_subtotal)||"Ara Toplam"}:</span>
                    <span>${p(e.subtotal,e.currency)}</span>
                </div>
                ${e.tax_amount>0?`
                    <div class="d-flex justify-content-between align-items-center mb-1">
                        <span>${e.tax_name||((i=window.translations)==null?void 0:i.store_basket_tax)||"Vergi"}:</span>
                        <span>${p(e.tax_amount,e.currency)}</span>
                    </div>
                `:""}
                <div class="d-flex justify-content-between align-items-center fw-bold">
                    <span>${((r=window.translations)==null?void 0:r.store_basket_total)||"Toplam"}:</span>
                    <span>${p(e.total,e.currency)}</span>
                </div>
            </div>
        `):n=`
            <div class="text-center py-3 text-muted">
                <i class="fas fa-shopping-cart fa-2x mb-2"></i>
                <div>${((d=window.translations)==null?void 0:d.store_basket_empty)||"Sepetiniz boş"}</div>
            </div>
        `,t.innerHTML=n}function u(e){return String(e??"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function F(e){var r,d,s;if(!e)return"";const t=((r=window.translations)==null?void 0:r.common_yes)||"Evet",n=((d=window.translations)==null?void 0:d.common_no)||"Hayır",a=((s=window.translations)==null?void 0:s.store_basket_custom_field)||"Ek Bilgi";let i="";return Array.isArray(e)?(e.forEach(l=>{let o=l.type==="checkbox"?String(l.value)==="1"?t:n:l.value;if(o==null||String(o).trim()==="")return;const c=l.label||a;i+=`<div class="text-muted small">+ <strong>${u(c)}</strong>: ${u(o)}</div>`}),i):(Object.entries(e).forEach(([l,o])=>{o&&o.toString().trim()&&(i+=`<div class="text-muted small">+ <strong>${u(a)}</strong>: ${u(o)}</div>`)}),i)}function L(e){var a;if(!e||e.length===0)return"";let t="";const n=e.slice(0,2);return n.length>0&&(t+=`<div class="text-muted small">+ <strong>${((a=window.translations)==null?void 0:a.store_basket_nameservers)||"Nameservers"}</strong>: ${n.join(", ")}</div>`),t}function p(e,t){return typeof t=="object"&&t.prefix?`${t.prefix}${parseFloat(e).toFixed(2)}`:typeof t=="object"&&t.suffix?`${parseFloat(e).toFixed(2)}${t.suffix}`:typeof t=="object"&&t.code?`${t.code} ${parseFloat(e).toFixed(2)}`:typeof t=="string"?`${t} ${parseFloat(e).toFixed(2)}`:`₺${parseFloat(e).toFixed(2)}`}function h(){var e;if(console.log("Window routes:",window.routes),console.log("Basket summary URL:",(e=window.routes)==null?void 0:e.basketSummary),console.log("Initializing basket count..."),!window.routes||!window.routes.basketSummary){console.log("Routes not loaded yet, waiting...");return}g(),window.addEventListener("basketUpdated",function(t){console.log("Basket updated event received:",t.detail),window.basketCache.summary=null,window.basketCache.lastUpdated=null,m()}),document.addEventListener("mouseenter",function(t){t.target&&t.target.closest&&t.target.closest("#basketButton")&&(clearTimeout(k),C())},!0),document.addEventListener("mouseleave",function(t){t.target&&t.target.closest&&t.target.closest("#basketButton, #basketPopup")&&(k=setTimeout(()=>{f()},300))},!0),document.addEventListener("click",function(t){t.target&&t.target.closest&&t.target.closest("#closeBasketPopup")&&f()})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",h):h();window.updateBasketCount=g;window.refreshBasketCache=B;window.showBasketPopup=C;window.hideBasketPopup=f;window.initializeBasketAfterRoutes=function(){var e;console.log("Routes loaded, initializing basket..."),console.log("Window routes:",window.routes),console.log("Basket summary URL:",(e=window.routes)==null?void 0:e.basketSummary),window.routes&&window.routes.basketSummary&&g()};
