import"./hoisted.yFz1BYXO.js";const f=window.__PW_API||"https://api.performancewest.net";function b(e){return new URLSearchParams(window.location.search).get(e)??""}const u=b("order"),p=b("token");p&&sessionStorage.setItem("pw_portal_token",p);const E=p||sessionStorage.getItem("pw_portal_token")||"",m={"Content-Type":"application/json"};E&&(m.Authorization=`Bearer ${E}`);let s=null,g="",l="";function i(e){for(const n of["loading","unit","did","confirm","success","error"]){const t=n==="loading"||n==="error"?"state-":"step-";document.getElementById(`${t}${n}`)?.classList.toggle("hidden",n!==e)}}u?(async()=>{try{const e=await fetch(`${f}/api/v1/portal/setup-info?order_id=${u}`,{headers:m});if(!e.ok)throw new Error("Order not found");if(s=await e.json(),s.selected_unit&&s.selected_did){i("success");return}if(s.has_own_ca_address)w(),i("did");else{const n=document.getElementById("unit-location-label");n&&s.amb_location&&(n.textContent=`Location: ${s.amb_location.name}, ${s.amb_location.city}, BC`),v(),i("unit")}}catch{i("error")}})():i("error");async function v(){try{const t=(await(await fetch(`${f}/api/v1/portal/setup-units?order_id=${u}`,{headers:m})).json()).units||[],a=document.getElementById("unit-select"),o=document.getElementById("unit-loading"),c=document.getElementById("unit-list");if(t.length===0){const d=document.getElementById("unit-error");d&&(d.textContent="No mailbox numbers available at this location right now. Please try again later or contact support.",d.classList.remove("hidden")),o&&o.classList.add("hidden");return}a.innerHTML='';for(const d of t){const r=document.createElement("option");r.value=d,r.textContent=`Unit ${d}`,a.appendChild(r)}o&&o.classList.add("hidden"),c&&c.classList.remove("hidden")}catch{const e=document.getElementById("unit-error");e&&(e.textContent="Could not load available mailbox numbers.",e.classList.remove("hidden")),document.getElementById("unit-loading")?.classList.add("hidden")}}async function w(){try{const n=await(await fetch(`${f}/api/v1/portal/setup-dids`,{headers:m})).json(),t=document.getElementById("did-list"),a=document.getElementById("did-loading");t.innerHTML="";for(const o of n.area_codes||[]){const c=n.dids?.[o]||[];if(!c.length)continue;const d=document.createElement("div");d.innerHTML=`

${o} Area Code

`;const r=document.createElement("div");r.className="grid grid-cols-2 gap-2";for(const y of c){const h=document.createElement("label");h.className="flex items-center gap-2 p-2.5 rounded-lg border border-gray-200 cursor-pointer hover:border-pw-400 has-[:checked]:border-pw-500 has-[:checked]:bg-pw-50 transition-colors",h.innerHTML=` ${y.formatted} `,r.appendChild(h)}d.appendChild(r),t.appendChild(d)}if(a&&a.classList.add("hidden"),t.classList.remove("hidden"),!t.children.length){const o=document.getElementById("did-error");o&&(o.textContent="No available Canadian numbers found. Please contact support.",o.classList.remove("hidden"))}}catch{document.getElementById("did-loading")?.classList.add("hidden");const e=document.getElementById("did-error");e&&(e.textContent="Could not search available numbers.",e.classList.remove("hidden"))}}document.getElementById("btn-unit-next")?.addEventListener("click",()=>{if(g=document.getElementById("unit-select")?.value||"",!g){const e=document.getElementById("unit-error");e&&(e.textContent="Please select a mailbox number.",e.classList.remove("hidden"));return}w(),i("did")});document.getElementById("btn-did-back")?.addEventListener("click",()=>{s?.has_own_ca_address||i("unit")});document.getElementById("btn-did-next")?.addEventListener("click",()=>{if(l=document.querySelector('input[name="did_choice"]:checked')?.value||"",!l){const t=document.getElementById("did-error");t&&(t.textContent="Please select a phone number.",t.classList.remove("hidden"));return}const e=document.getElementById("confirm-unit-row");if(s?.has_own_ca_address)e&&e.classList.add("hidden");else{const t=document.getElementById("confirm-unit");t&&(t.textContent=`${s?.amb_location?.name||"Location"}, Unit ${g}`)}const n=document.getElementById("confirm-did");if(n){const t=l.replace(/\D/g,"");n.textContent=t.length>=10?`+1 (${t.slice(-10,-7)}) ${t.slice(-7,-4)}-${t.slice(-4)}`:l}i("confirm")});document.getElementById("btn-change-unit")?.addEventListener("click",()=>i("unit"));document.getElementById("btn-change-did")?.addEventListener("click",()=>i("did"));document.getElementById("btn-confirm")?.addEventListener("click",async()=>{const e=document.getElementById("btn-confirm"),n=document.getElementById("confirm-status");e.disabled=!0,e.textContent="Confirming...",n&&(n.textContent="Sending your selections...",n.className="mt-3 text-sm text-center text-gray-500",n.classList.remove("hidden"));try{const t=await fetch(`${f}/api/v1/portal/setup-confirm`,{method:"POST",headers:m,body:JSON.stringify({order_id:u,selected_unit:g||null,selected_did:l})}),a=await t.json();if(t.ok&&a.success)i("success");else throw new Error(a.error||"Confirmation failed")}catch(t){n&&(n.textContent=t.message||"Something went wrong.",n.className="mt-3 text-sm text-center text-red-600",n.classList.remove("hidden")),e.disabled=!1,e.innerHTML=' Confirm & Begin Setup'}});