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