import{n as e,s as t}from"./preload-helper-CggyeTPP.js";import{n,r,t as i}from"./voucher-4ZssGMfe.js";var a=`account.session`,o=`account.entitlement`,s=1440*60,c=2e3,l=250,u=2e3;async function d(t,n){return fetch(`${e}${t}`,{...n,headers:{"Content-Type":`application/json`,...n.headers??{}}})}async function f(){let e=(await t.storage.local.get(a))[a];return e&&typeof e==`object`?e:null}async function p(e){e?await t.storage.local.set({[a]:e}):await t.storage.local.remove(a)}async function m(e){try{let t=await d(`/v1/auth/magic`,{method:`POST`,body:JSON.stringify({email:e})}),n=await t.json().catch(()=>({}));return t.ok?{ok:!0}:{ok:!1,error:n.error??`http_${t.status}`}}catch{return{ok:!1,error:`network`}}}async function h(e,t){try{let n=await d(`/v1/auth/verify`,{method:`POST`,body:JSON.stringify({email:e,code:t})}),r=await n.json().catch(()=>({}));return!n.ok||!r.access||!r.refresh?{ok:!1,error:r.error??`http_${n.status}`}:(await p({access:r.access,refresh:r.refresh,email:r.email??e}),await C(),{ok:!0})}catch{return{ok:!1,error:`network`}}}async function g(){let e=await f(),n=new AbortController,r=setTimeout(()=>n.abort(),c);try{e&&await d(`/v1/auth/logout`,{method:`POST`,headers:{Authorization:`Bearer ${e.access}`},body:JSON.stringify({refresh:e.refresh}),signal:n.signal})}catch{}finally{clearTimeout(r),await p(null),await t.storage.local.remove(o)}}function _(e){try{let t=e.split(`.`)[1]??``,n=JSON.parse(atob(t.replace(/-/g,`+`).replace(/_/g,`/`)));return typeof n.exp==`number`?n.exp:0}catch{return 0}}function v(e){return typeof e!=`number`||!Number.isFinite(e)?l:Math.min(u,Math.max(0,Math.trunc(e)))}function y(e){return new Promise(t=>setTimeout(t,e))}async function b(){let e=await f();if(!e)return null;if(_(e.access)-60>Date.now()/1e3)return e.access;try{for(let t=0;t<2;t++){let n=await d(`/v1/auth/refresh`,{method:`POST`,body:JSON.stringify({refresh:e.refresh})}),r=await n.json().catch(()=>({}));if(n.status===401||n.status===403)return await g(),null;if(n.status===409&&r.error===`refresh_conflict`){if(t>0)return null;await y(v(r.retry_after_ms));let n=await f();if(!n)return null;if(_(n.access)-60>Date.now()/1e3)return n.access;e=n;continue}return!n.ok||!r.access||!r.refresh?null:(await p({...e,access:r.access,refresh:r.refresh,email:r.email??e.email}),r.access)}return null}catch{return null}}var x=r;async function S(){let e=(await t.storage.local.get(o))[o];return typeof e?.token==`string`?e.token:null}async function C(){let e=await b();if(!e)return null;try{let r=await d(`/v1/billing/entitlement`,{method:`GET`,headers:{Authorization:`Bearer ${e}`}});if(!r.ok)return null;let i=await r.json();if(typeof i.token!=`string`)return null;let a=await n(i.token);return a?(await t.storage.local.set({[o]:{token:i.token}}),a):null}catch{return null}}async function w(){let e=await S(),t=e?await n(e):null;if(t&&Date.now()/1e3-t.iat<s)return x(t);let r=await C();return r?x(r):t?x(t):!1}async function T(){let e=await b();if(!e)return{ok:!1,error:`not_signed_in`};try{let r=await d(`/v1/trial/start`,{method:`POST`,headers:{Authorization:`Bearer ${e}`},body:JSON.stringify({product:i[0]})}),a=await r.json().catch(()=>({}));return r.ok?(typeof a.token==`string`&&await n(a.token)&&await t.storage.local.set({[o]:{token:a.token}}),{ok:!0,started:!!a.started,expiresAt:a.entitlement?.expires_at??null}):{ok:!1,error:a.error??`http_${r.status}`}}catch{return{ok:!1,error:`network`}}}export{h as a,S as c,m as i,f as n,g as o,C as r,T as s,w as t};