class g{constructor(){this.init()}init(){this.loadAccounts(),this.bindEvents()}async loadAccounts(){var n,e;try{const c=await fetch(((n=window.routes)==null?void 0:n.accountList)||"/clientarea/account-list",{headers:{"X-Requested-With":"XMLHttpRequest","X-CSRF-TOKEN":window.csrfToken||((e=document.querySelector('meta[name="csrf-token"]'))==null?void 0:e.getAttribute("content"))}});if(!c.ok)throw new Error("Failed to load accounts");const o=await c.json();console.log("Loaded accounts:",o),this.renderAccountList(o.accounts,o.active_account_id)}catch(c){console.error("Error loading accounts:",c),this.showError()}}renderAccountList(n,e){const c=document.getElementById("accountList");if(!c)return;console.log("Rendering accounts:",n,"Active:",e);const o=c.querySelector(".text-center");o&&o.remove(),c.querySelectorAll("li a[data-account-id]").forEach(t=>{t.closest("li").remove()});const s=n.find(t=>t.id==e);if(s){const t=document.getElementById("currentAccountName");if(t&&(t.replaceChildren(document.createTextNode(s.name)),!s.is_own)){t.appendChild(document.createTextNode(" "));const i=document.createElement("small");i.className="text-warning";const d=document.createElement("i");d.className="fas fa-exchange-alt",i.appendChild(d),t.appendChild(i)}}let h=c.querySelector(".dropdown-header").closest("li").nextElementSibling;n.sort((t,i)=>t.is_own&&!i.is_own?-1:!t.is_own&&i.is_own?1:0).forEach(t=>{var p;const i=document.createElement("li"),d=t.id==e,a=document.createElement("a");a.className="dropdown-item d-flex align-items-center"+(d?" active bg-primary text-white":""),a.href="#",a.dataset.accountId=t.id,a.addEventListener("click",u=>{u.preventDefault(),window.accountSwitcher.switchAccount(t.id)});const f=document.createElement("i");f.className="fas "+(t.is_own?"fa-user":"fa-user-friends")+" me-2",a.appendChild(f);const l=document.createElement("div");l.className="flex-grow-1";const m=document.createElement("div");m.className="fw-medium";const E=t.is_own?" ("+(((p=window.translations)==null?void 0:p.ownAccount)||"Main Account")+")":"";m.textContent=t.name+E,l.appendChild(m);const w=document.createElement("small");if(w.className=d?"text-white-50":"text-muted",w.textContent=t.email,l.appendChild(w),a.appendChild(l),d){const u=document.createElement("i");u.className="fas fa-check text-success ms-2",a.appendChild(u)}i.appendChild(a),h.insertAdjacentElement("afterend",i),h=i})}async switchAccount(n){var e,c,o;try{const r=await fetch(((e=window.routes)==null?void 0:e.switchAccount)||"/clientarea/switch-account",{method:"POST",headers:{"Content-Type":"application/json","X-Requested-With":"XMLHttpRequest","X-CSRF-TOKEN":window.csrfToken||((c=document.querySelector('meta[name="csrf-token"]'))==null?void 0:c.getAttribute("content"))},body:JSON.stringify({client_id:n})}),s=await r.json();if(!r.ok)throw new Error(s.error||"Failed to switch account");this.showSuccess(s.message),setTimeout(()=>{s.redirect_url?window.location.href=s.redirect_url:window.location.reload()},1e3)}catch(r){console.error("Error switching account:",r),window.showError(((o=window.translations)==null?void 0:o.switchAccountError)||"Error switching account: "+r.message)}}bindEvents(){const n=document.getElementById("accountSwitcher");n&&n.addEventListener("show.bs.dropdown",()=>{document.querySelector("#accountList .spinner-border")&&this.loadAccounts()})}showError(){const n=document.getElementById("accountList");if(!n)return;const e=n.querySelector(".text-center");e&&e.remove()}showSuccess(n){var c;const e=document.querySelector("#accountSwitcher");if(e){const o=[...e.childNodes];e.replaceChildren();const r=document.createElement("i");r.className="fas fa-check text-success me-1",e.appendChild(r);const s=document.createElement("span");s.textContent=n||((c=window.translations)==null?void 0:c.switchAccountSuccess)||"Switched successfully",e.appendChild(s),setTimeout(()=>{e.replaceChildren(...o)},2e3)}}}document.addEventListener("DOMContentLoaded",function(){document.getElementById("accountSwitcher")&&(window.accountSwitcher=new g)});
