import"./site-config.js";import{authSiteName as w,AUTH_CONFIG as r}from"./auth/auth-config.js";import{authTelemetryLog as c,authTelemetryDump as f,authTelemetryClear as v}from"./auth/auth-telemetry.js";import{hasAnyEntitlement as y,resolveEntitlementNames as i}from"./auth/auth-token.js";import{getOrCreateDeviceId as u,readStoredAuth as m}from"./auth/auth-storage.js";import{requestSignInOtp as d,login as g,checkEntitlement as o,trialStatus as A,trialConsume as k,registerAuthMessageListener as p,storageKeys as a}from"./auth/auth-api.js";async function h(){const t=await m(a),e=t.sessionToken;if(!e)return{ok:!1,remaining:0};const s=t.deviceId||await u(a);return A({baseUrl:r.baseUrl,sessionToken:e,deviceId:s,entitlement:r.entitlementName})}async function b(){const t=await m(a),e=t.sessionToken;if(!e)throw new Error("Please sign in to start your free trial.");const s=t.deviceId||await u(a);return k({baseUrl:r.baseUrl,sessionToken:e,deviceId:s,entitlement:r.entitlementName})}const N={requestCode:async t=>{if(!t)throw new Error("Email is required");c("auth.otp.request",{email:t,ctx:r.storagePrefix});try{return await d({baseUrl:r.baseUrl,email:t}),c("auth.otp.sent",{email:t,ctx:r.storagePrefix}),!0}catch(e){const s=e instanceof Error?e.message:String(e);throw c("auth.otp.error",{email:t,error:s,ctx:r.storagePrefix}),e}},activate:async({email:t,code:e})=>{if(!t||!e)throw new Error("Email and code are required");return g(t,e,a)},activateLicense:async(t,e)=>{if(!e)return{success:!1,error:"Email is required"};if(!t)return await d({baseUrl:r.baseUrl,email:e}),{success:!1,error:"Code sent. Please enter the verification code."};try{const s=await g(e,t,a);return y(s.entitlements||s.token,r.entitlementName,r.entitlementAliases)?(await chrome.storage.local.set({[a.activatedFlag]:!0}),{success:!0}):(await chrome.storage.local.set({[a.activatedFlag]:!1}),{success:!1,error:`This email does not have access to ${w}.`})}catch(s){return{success:!1,error:s?.message||"Activation failed"}}},hasEntitlement:async t=>!!(await o(t||i(r.entitlementName,r.entitlementAliases),a)).ok,checkActivation:async()=>{if((await o(i(r.entitlementName,r.entitlementAliases),a)).ok)return{isActivated:!0,mode:"paid"};try{const e=await h(),s=Number(e?.remaining)||0,n=!!e?.ok&&s>0;return await chrome.storage.local.set({[a.activatedFlag]:n}),{isActivated:n,mode:n?"trial":"none",remaining:s}}catch{return await chrome.storage.local.set({[a.activatedFlag]:!1}),{isActivated:!1,mode:"none"}}},checkActivationStatus:async()=>{const t=await o(i(r.entitlementName,r.entitlementAliases),a),e=await m(a);if(t.ok)return await chrome.storage.local.set({[a.activatedFlag]:!0}),{isActivated:!0,mode:"paid",remaining:null,licenseKey:null,email:e.email||null};try{const s=await h(),n=Number(s?.remaining)||0,l=!!s?.ok&&n>0;return await chrome.storage.local.set({[a.activatedFlag]:l}),{isActivated:l,mode:l?"trial":"none",remaining:n,licenseKey:null,email:e.email||null}}catch{return await chrome.storage.local.set({[a.activatedFlag]:!1}),{isActivated:!1,mode:"none",remaining:null,licenseKey:null,email:e.email||null}}},ensureDownloadAccess:async()=>{if((await o(i(r.entitlementName,r.entitlementAliases),a)).ok)return await chrome.storage.local.set({[a.activatedFlag]:!0}),{ok:!0,mode:"paid",remaining:null};const e=await b(),s=!!e?.allowed,n=Number(e?.remaining)||0;if(await chrome.storage.local.set({[a.activatedFlag]:s}),!s)throw new Error("Free trial used up. Please purchase access to continue.");return{ok:!0,mode:"trial",remaining:n}},deactivate:async()=>{try{await chrome.storage.local.remove(Object.values(a))}catch{}},registerAuthMessageListener:()=>p(a),telemetryDump:()=>f(),telemetryClear:()=>v(),storageKeys:a,config:r};globalThis.Auth=N;
