class l extends Error{constructor(e,n){super(n),this.status=e}status}async function i(t,e={}){const n=await fetch(`/api${t}`,{...e,headers:{...e.body?{"content-type":"application/json"}:{},...e.headers??{}},credentials:"same-origin"}),r=await n.json().catch(()=>({}));if(!n.ok)throw new l(n.status,r.error??`request failed (${n.status})`);return r}const m=(t,e={})=>i(t,{method:"POST",body:JSON.stringify(e)});function o(t=32){const e=new Uint8Array(t);return crypto.getRandomValues(e),c(e)}function c(t){let e="";for(const n of t)e+=String.fromCharCode(n);return btoa(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function d(t){const e=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(t));return c(new Uint8Array(e))}const u=()=>`${location.origin}/account/`;async function g(t=`/account/${location.hash}`){const e=await i("/portal/config"),n=o(48),r=o(16);sessionStorage.setItem("kchat.pkce",JSON.stringify({verifier:n,state:r,next:t}));const s=new URL(e.authorizationEndpoint);s.searchParams.set("response_type","code"),s.searchParams.set("client_id",e.clientId),s.searchParams.set("redirect_uri",u()),s.searchParams.set("scope","openid email profile"),s.searchParams.set("state",r),s.searchParams.set("code_challenge",await d(n)),s.searchParams.set("code_challenge_method","S256"),location.assign(s.toString())}async function f(t,e){const n=sessionStorage.getItem("kchat.pkce");sessionStorage.removeItem("kchat.pkce");const r=n?JSON.parse(n):void 0;if(!r||r.state!==e)throw new Error("sign-in did not match this browser tab; try again");const s=await m("/portal/session",{code:t,codeVerifier:r.verifier,redirectUri:u()}),a=r.next??"/account/";return history.replaceState(null,"",a.startsWith("/account/")?a:"/account/"),{me:s,next:a}}function h(t,e={},...n){const r=document.createElement(t);for(const[s,a]of Object.entries(e))a!==!1&&(s==="class"?r.className=String(a):s==="text"?r.textContent=String(a):r.setAttribute(s,a===!0?"":a));for(const s of n)s==null||s===!1||r.append(s);return r}const p=t=>`₹${t.toLocaleString("en-IN",{maximumFractionDigits:0})}`,S=(t,e,n,r)=>Math.round(t*e*(1+n/100)*(1+r/100)),y=t=>`$${t.toLocaleString("en-US",{minimumFractionDigits:0,maximumFractionDigits:Number.isInteger(t)?0:2})}`,w=t=>t?new Date(t).toLocaleDateString("en-IN",{day:"numeric",month:"short",year:"numeric"}):"",b=t=>{const e=Math.abs(t),n=e.toLocaleString("en-US",{minimumFractionDigits:2,maximumFractionDigits:e<1&&e!==0?4:2});return`${t<0&&n!=="0.00"?"-":""}$${n}`};function x(t,e){return h("p",{class:`notice notice-${t}`,role:t==="error"?"alert":"status",text:e})}export{l as A,i as a,b,w as d,f,h,p as m,x as n,m as p,S as r,g as s,y as u};