import"./hoisted.yFz1BYXO.js";const f=window.__PW_API||"https://api.performancewest.net";function c(e){return new URLSearchParams(window.location.search).get(e)??""}function i(e){["loading","success","error"].forEach(t=>{const n=document.getElementById(`state-${t}`);n&&n.classList.toggle("hidden",t!==e)})}function w(e){const n=["canada_crtc","formation"].includes(e)?e:"generic";["canada_crtc","formation","generic"].forEach(r=>{const s=document.getElementById(`next-steps-${r}`);s&&s.classList.toggle("hidden",r!==n)})}let y="",E="";document.getElementById("btn-set-portal-password")?.addEventListener("click",async()=>{const e=document.getElementById("btn-set-portal-password"),t=document.getElementById("portal-password"),n=document.getElementById("portal-password-confirm"),r=document.getElementById("portal-password-error"),s=t.value.trim(),o=n.value.trim();if(r&&r.classList.add("hidden"),!s||s.length<8){r&&(r.textContent="Password must be at least 8 characters.",r.classList.remove("hidden")),t.focus();return}if(s!==o){r&&(r.textContent="Passwords do not match.",r.classList.remove("hidden")),n.focus();return}if(!y){r&&(r.textContent="Could not identify your account. Please use the email link sent to your inbox.",r.classList.remove("hidden"));return}e.disabled=!0,e.textContent="Activating...";try{const a=await fetch(f+"/api/v1/auth/set-erpnext-password",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:y,password:s,customer_name:E})}),d=await a.json();if(a.ok){const l=document.getElementById("portal-password-form"),u=document.getElementById("portal-already-set"),m=document.getElementById("portal-login-link");l&&l.classList.add("hidden"),u&&u.classList.remove("hidden"),m&&(m.href="https://portal.performancewest.net")}else r&&(r.textContent=d.error||"Failed to activate portal. Please try again.",r.classList.remove("hidden")),e.disabled=!1,e.textContent="Activate Portal Account"}catch{r&&(r.textContent="Network error. Please try again.",r.classList.remove("hidden")),e.disabled=!1,e.textContent="Activate Portal Account"}});async function h(e,t=0){try{const s=await fetch(f+`/api/v1/checkout/session/${encodeURIComponent(e)}`);if(!s.ok)throw new Error(`HTTP ${s.status}`);const o=await s.json();if(o.status==="paid"){const a=o.order_id??c("order_id"),d=o.order_type??c("order_type"),l=o.customer_email??sessionStorage.getItem("pw_checkout_email")??"",u=o.customer_name??"";y=l,E=u;const m=document.getElementById("order-id-display");m&&a&&(m.textContent=`Order ID: ${a}`);const g=document.getElementById("success-email");g&&(g.textContent=l||"your email address"),w(d),i("success"),sessionStorage.removeItem("pw_order_number"),sessionStorage.removeItem("pw_checkout_email");return}if(o.status==="failed"||o.status==="expired"){const a=c("order_id"),d=document.getElementById("error-order-id-display");d&&a&&(d.textContent=`Order ID: ${a}`),i("error");return}if(t<11)setTimeout(()=>h(e,t+1),2e3);else{const a=c("order_id"),d=document.getElementById("error-order-id-display");d&&a&&(d.textContent=`Order ID: ${a}`),i("error")}}catch{t<11?setTimeout(()=>h(e,t+1),2e3):i("error")}}document.getElementById("btn-error-retry")?.addEventListener("click",async()=>{const e=document.getElementById("btn-error-retry"),t=document.getElementById("error-retry-status"),n=document.querySelector('input[name="error_retry_method"]:checked')?.value||"card",r=c("order_id"),s=c("order_type")||"canada_crtc";if(!r){t&&(t.textContent="Order ID missing — please contact support.",t.className="mt-2 text-sm text-red-600 text-center",t.classList.remove("hidden"));return}e.disabled=!0,e.textContent="Redirecting...",t&&(t.textContent="Creating checkout session...",t.className="mt-2 text-sm text-gray-500 text-center",t.classList.remove("hidden"));try{const o=await fetch(f+"/api/v1/checkout/create-session",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({order_id:r,order_type:s,payment_method:n})}),a=await o.json(),d=a.url||a.checkout_url||a.redirect_url;if(o.ok&&d)t&&(t.textContent="Redirecting to payment..."),window.location.href=d;else throw new Error(a.error||"Failed to create checkout session")}catch(o){t&&(t.textContent=o.message||"Something went wrong.",t.className="mt-2 text-sm text-red-600 text-center",t.classList.remove("hidden")),e.disabled=!1,e.innerHTML=' Retry Payment'}});async function I(e,t){try{const n=c("token");if(!n){i("error");const o=document.getElementById("error-order-id-display");o&&(o.textContent=`Order ID: ${e}`);return}const s=await(await fetch(f+"/api/v1/paypal/capture",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({paypal_order_id:n,order_id:e,order_type:t})})).json();if(s.success||s.already_captured){const o=document.getElementById("order-id-display");o&&(o.textContent=`Order ID: ${e}`);const a=document.getElementById("success-email");a&&(a.textContent=s.payer_email||sessionStorage.getItem("pw_checkout_email")||"your email address"),y=s.payer_email||sessionStorage.getItem("pw_checkout_email")||"",w(t),i("success"),sessionStorage.removeItem("pw_order_number"),sessionStorage.removeItem("pw_checkout_email")}else{console.error("[paypal] Capture failed:",s);const o=document.getElementById("error-order-id-display");o&&(o.textContent=`Order ID: ${e}`),i("error")}}catch(n){console.error("[paypal] Capture error:",n);const r=document.getElementById("error-order-id-display");r&&(r.textContent=`Order ID: ${e}`),i("error")}}const x=c("paypal")==="1",_=c("session_id"),p=c("order_id"),v=c("order_type")||"canada_crtc",C=c("expedited")==="1";if(C){document.querySelectorAll("#error-retry-methods > label").forEach(t=>{const n=t.querySelector("input[type='radio']");n&&n.value!=="paypal"&&n.value!=="crypto"&&(t.classList.add("hidden"),n.checked=!1)});const e=document.querySelector('input[name="error_retry_method"][value="paypal"]');e&&(e.checked=!0),document.getElementById("expedited-error-note")?.classList.remove("hidden")}if(x&&p)I(p,v);else if(_)h(_);else{const e=document.getElementById("error-order-id-display");e&&p&&(e.textContent=`Order ID: ${p}`),i("error")}