import{bo as g,be as y,bh as a,bd as m,br as T}from"./index-CYLYBVPp.js";const l="_llg_StatisticsModel",w="_llg_UserModel";function N(){const n=Math.floor(Date.now()/1e3);return`g_${Math.random().toString(36).substring(2,12)}_${n}`}function M(n){const e=String(n);return typeof Buffer<"u"?Buffer.from(e,"utf8").toString("base64url"):btoa(unescape(encodeURIComponent(e))).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function O(n,e){if(!n)throw new Error("statsClientId is required");return e==null||e===""||e===-1||e==="-1"?n:`${n}_${M(e)}`}const R="https://www.google-analytics.com/mp/collect",A="https://www.google-analytics.com/debug/mp/collect",b="G-RRWEEWZPMH",S="g3u0K5LrQkCBunwdITQftw",h=!1,L=30,D=100,c="ga_session_data";function C(){return!!S}function k(){return!C()}async function U(){const e=(await chrome.storage.local.get(l))[l];if(e!=null&&e.clientId)return e.clientId;const t=N();return await chrome.storage.local.set({[l]:{...e&&typeof e=="object"?e:{},clientId:t}}),t}async function x(){var u,d,f,_,E,I;const e=(await chrome.storage.local.get(w))[w],t=(d=(u=e==null?void 0:e.jwt)==null?void 0:u[0])==null?void 0:d.token,r=(E=(_=(f=e==null?void 0:e.jwt)==null?void 0:f[0])==null?void 0:_.payload)==null?void 0:E.exp;if(!t||!r)return null;const s=Math.floor(Date.now()/1e3);if(r<=s)return null;const o=((I=e==null?void 0:e.user)==null?void 0:I.ID)??(e==null?void 0:e.id);return o==null||o===""||o===-1||o==="-1"?null:o}async function G(){const n=await U(),e=await x();return O(n,e)}async function v(){const{[c]:n}=await chrome.storage.session.get(c),e=Date.now();if(n!=null&&n.timestamp&&(e-Number(n.timestamp))/6e4<=L){const s={...n,timestamp:e};return await chrome.storage.session.set({[c]:s}),s.session_id}const t={session_id:String(e),timestamp:e};return await chrome.storage.session.set({[c]:t}),t.session_id}function P(){var n;try{return((n=chrome.runtime.getManifest())==null?void 0:n.version)||"unknown"}catch{return"unknown"}}async function i(n,e={}){if(k()||!n)return{ok:!1,skipped:!0};try{const t=await G(),r=await v(),s=h?A:R,o={client_id:t,user_properties:{lingoway_user_id:{value:t}},events:[{name:n,params:{session_id:r,engagement_time_msec:D,extension_version:P(),...e,lingoway_user_id:t}}]};return{ok:(await fetch(`${s}?measurement_id=${b}&api_secret=${S}`,{method:"POST",body:JSON.stringify(o)})).ok}}catch(t){return console.debug("[ga] track failed",n,t),{ok:!1,error:t}}}function p(n,e="/home",t="icon"){n!=null&&n.id&&(i(a.DRAWER_OPEN,{source:t,path:e}),chrome.tabs.sendMessage(n.id,{type:g.OPEN_DRAWER,path:e}).catch(()=>{}))}function B(){chrome.action.onClicked.addListener(n=>{p(n,"/home","icon")}),chrome.commands.onCommand.addListener(n=>{n==="_execute_action"&&chrome.tabs.query({active:!0,currentWindow:!0},e=>{p(e[0],"/home","command")})})}function $(){chrome.runtime.onMessage.addListener(function(n,e,t){const{contentRequest:r}=n;return n.type===g.TRACK_EVENT?(i(n.name,n.params||{}).then(s=>{t(s)}),!0):r===g.API_REQUEST?(chrome.tabs.query({currentWindow:!0,active:!0},function(){let{config:s}=n;s.success=o=>{t({result:"succ",data:o})},s.fail=o=>{t({result:"fail",msg:typeof o=="string"?o:o==null?void 0:o.message})},y.requestDirect(s)}),!0):!1})}function W(){chrome.runtime.onInstalled.addListener(function(n){if(m.set("lingoway_onInstalled",new Date().getTime()).then(()=>{console.log("lingoway onInstalled set")}),m.get("lingoway_onInstalled").then(e=>{console.log("lingoway onInstalled get",e)}),n.reason===chrome.runtime.OnInstalledReason.INSTALL){i(a.EXTENSION_INSTALL),chrome.runtime.setUninstallURL("https://forms.gle/BnmMeYCfBVpz9xLy6"),chrome.tabs.create({url:"https://lingoway.net/try",active:!0});return}n.reason===chrome.runtime.OnInstalledReason.UPDATE&&i(a.EXTENSION_UPDATE,{previous_version:n.previousVersion||"unknown"})})}function K(){chrome.runtime.onMessageExternal.addListener(function(n,e,t){console.log("onMessageExternal_sender",n,e),m.set("lingoway_onMessageExternal_request",n).then(()=>{t(!0)})})}B();W();$();K();T({context:"background",send:n=>{i(a.EXTENSION_ERROR,n)}});
