import{AUTH_CONFIG as o}from"./auth-config.js";const a=`telemetry:auth:${o.storagePrefix}`,c=300,h="/telemetry/auth";function l(t){if(!t||typeof t!="object")return t;const e={...t};return Object.keys(e).forEach(r=>{/token|authorization|cookie/i.test(r)&&(e[r]="[redacted]")}),e}async function i(){try{const t=await chrome.storage.local.get(a),e=t&&t[a];return Array.isArray(e)?e:[]}catch{return[]}}async function s(t){try{await chrome.storage.local.set({[a]:t})}catch{}}function T(t,e){try{typeof chrome<"u"&&chrome.runtime?.sendMessage&&chrome.runtime.sendMessage({type:"TELEMETRY_LOG",evt:t,source:"auth",data:e})}catch{}}function f(){try{if(globalThis.SiteConfig&&globalThis.SiteConfig.AUTH_TELEMETRY_URL)return String(globalThis.SiteConfig.AUTH_TELEMETRY_URL);const t=String(o.baseUrl||"").replace(/\/$/,"");return t?`${t}${h}`:null}catch{return null}}function y(t){return String(t||"").includes(".error")||String(t||"").endsWith(".failed")}function m(t,e){const r=f();if(r)try{fetch(r,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({ts:Date.now(),evt:t,ctx:o.storagePrefix,data:e}),keepalive:!0,mode:"no-cors"}).catch(()=>{})}catch{}}export function authTelemetryLog(t,e){const r=l({...e,ctx:o.storagePrefix}),u={ts:Date.now(),evt:t,source:"auth",ctx:o.storagePrefix,data:r};T(t,r),y(t)&&m(t,r),(async()=>{const n=await i();n.push(u),n.length>c&&n.splice(0,n.length-c),await s(n)})()}export async function authTelemetryDump(){return await i()}export async function authTelemetryClear(){await s([])}
