!function(){"use strict";function e(t,a,i,n){let s=window.aipkit_token_usage_config||{},o=s.text||{},d=document.createElement("div");d.className="aipkit-usage-details-table-container";let r=document.createElement("div");r.className="aipkit-usage-details-pagination-container",n.appendChild(d),n.appendChild(r),d.innerHTML=`<div class="aipkit-usage-details-loading"><span class="aipkit_spinner"></span><span>${o.loadingDetails||"Loading activity..."}</span></div>`;let l={action:"aipkit_get_token_usage_details",nonce:s.nonce,module:t,context_id:a,page:i},c=new FormData;for(let e in l)c.append(e,l[e]);fetch(s.ajaxUrl,{method:"POST",body:c,credentials:"same-origin"}).then(e=>e.json()).then(i=>{if(!i.success)throw new Error(i.data?.message||"Failed to load activity.");(function(e,t){if(!e||0===e.length)return void(t.innerHTML='<div class="aipkit-usage-details-empty">No detailed usage activity found for this period.</div>');let a='<table class="aipkit_usage_details_table"><thead><tr><th>Date</th><th>Usage</th></tr></thead><tbody>';e.forEach(e=>{let t=new Date(1e3*e.timestamp).toLocaleString(void 0,{dateStyle:"medium",timeStyle:"short"});a+=`<tr><td>${t}</td><td>${e.tokens.toLocaleString()}</td></tr>`}),a+="</tbody></table>",t.innerHTML=a})(i.data.items,d),function(t,a,i,n,s){let{currentPage:o,totalPages:d}=t,r=window.aipkit_token_usage_config?.text||{};if(a.innerHTML="",d<=1)return;let l=document.createElement("button");l.className="aipkit_btn aipkit_btn-secondary aipkit_btn-small",l.textContent=r.previous||"Previous",l.disabled=o<=1,l.addEventListener("click",()=>e(i,n,o-1,s)),a.appendChild(l);let c=document.createElement("span");c.className="aipkit_pagination-current",c.textContent=`${r.pageLabel||"Page"} ${o} ${r.ofLabel||"of"} ${d}`,a.appendChild(c);let u=document.createElement("button");u.className="aipkit_btn aipkit_btn-secondary aipkit_btn-small",u.textContent=r.next||"Next",u.disabled=o>=d,u.addEventListener("click",()=>e(i,n,o+1,s)),a.appendChild(u)}(i.data.pagination,r,t,a,n)}).catch(e=>{d.innerHTML=`<div class="aipkit-usage-details-error">${o.errorLoading||"Error loading activity."} (${e.message})</div>`})}function t(e,t="start"){!e||"function"!=typeof e.scrollIntoView||window.setTimeout(()=>{e.scrollIntoView({behavior:"smooth",block:t})},60)}function a(){if(!document.querySelector(".aipkit_token_usage_dashboard"))return;let e=new URLSearchParams(window.location.search),a=String(e.get("aipkit_section")||"").trim().toLowerCase();if(!a)return;if("credits"===a)return void t(document.getElementById("aipkit_customer_dashboard_credits"));if("purchases"===a){let e=document.querySelector(".aipkit_toggle_purchase_history"),a=document.getElementById("aipkit_purchase_history_details");return e&&a&&"true"!==e.getAttribute("aria-expanded")&&e.click(),void t(a||document.getElementById("aipkit_customer_dashboard_credits"))}if("usage"!==a)return;t(document.getElementById("aipkit_customer_dashboard_usage"));let i=String(e.get("aipkit_module")||"").trim(),n=String(e.get("aipkit_context_id")||"").trim();if(!i||!n)return;let s=Array.from(document.querySelectorAll(".aipkit-usage-details-btn")).find(e=>e.dataset.module===i&&e.dataset.contextId===n);s&&(s.classList.contains("aipkit-active")||s.click(),t(s.closest("tr")||s,"center"))}document.addEventListener("click",function(t){let a=t.target.closest(".aipkit-usage-details-btn");if(!a||a.disabled)return;let i=a.closest("tr.aipkit-usage-main-row");if(!i)return;let n=i.nextElementSibling,s=n&&n.classList.contains("aipkit-usage-details-row");if(document.querySelectorAll(".aipkit-usage-details-row").forEach(e=>{if(e!==n){let t=e.previousElementSibling.querySelector(".aipkit-usage-details-btn");t&&t.classList.remove("aipkit-active"),e.remove()}}),s)n.remove(),a.classList.remove("aipkit-active");else{a.classList.add("aipkit-active");let t=document.createElement("tr");t.className="aipkit-usage-details-row";let n=document.createElement("td");n.colSpan=i.cells.length,n.className="aipkit-usage-details-cell";let s=document.createElement("div");s.className="aipkit-usage-details-content",n.appendChild(s),t.appendChild(n),i.parentNode.insertBefore(t,i.nextSibling),e(a.dataset.module,a.dataset.contextId,1,s)}}),document.addEventListener("click",function(e){let t=e.target.closest(".aipkit_toggle_purchase_history");if(!t)return;e.preventDefault();let a=document.getElementById("aipkit_purchase_history_details");a&&("true"===t.getAttribute("aria-expanded")?(a.style.display="none",t.setAttribute("aria-expanded","false")):(a.style.display="block",t.setAttribute("aria-expanded","true")))}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",a,{once:!0}):a()}();