import{h as a,d as r}from"./usageLimits-D98f5ZF0.js";import{g as m}from"./authService-BmDo81L8.js";const g=600*1e3;function w(){return"https://select-ai.cn"}async function h(){const e=await chrome.storage.local.get([a.userType,a.dailyLimit,a.dailyUsed,"userInfoCacheTime"]),t=e.userInfoCacheTime,i=Date.now();if(!t||i-t>g)return null;const n=e[a.userType]??0,s=e[a.dailyLimit]??0,f=e[a.dailyUsed]??0;return{userType:n,dailyLimit:s,dailyUsed:f,isPro:n===2}}async function p(e){const t=new Date().toISOString().split("T")[0];await chrome.storage.local.set({[a.userType]:e.userType,[a.dailyLimit]:e.dailyLimit,[a.dailyUsed]:e.dailyUsed,[a.requestCount]:e.dailyUsed,[a.requestDate]:t,userInfoCacheTime:Date.now()})}async function l(){const e=await m();if(!e)return null;const t=w();try{const i=await fetch(`${t}/api/user`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!i.ok)return console.warn("[UserService] Failed to fetch user info:",i.status),null;const n=await i.json();if(!n.user)return null;const s={userType:n.user.user_type??0,dailyLimit:n.user.daily_limit??0,dailyUsed:n.user.daily_used??0,isPro:n.user.user_type===2};return await p(s),s}catch(i){return console.warn("[UserService] Error fetching user info:",i),null}}async function U(){const t=(await chrome.storage.local.get(["selectedProvider"])).selectedProvider;return!t||t==="free"?!1:!!(await chrome.storage.local.get([`${t}ApiKey`]))[`${t}ApiKey`]}async function D(){await U()||await h()||await l()}async function d(){await l()}function y(){return new Date().toISOString().split("T")[0]}async function u(){const e=await chrome.storage.local.get([r.dailyUsed,r.requestDate]),t=e[r.requestDate],i=y();return t!==i?(d().catch(()=>{}),0):e[r.dailyUsed]??0}async function I(){const e=await chrome.storage.local.get([r.dailyUsed,r.requestDate]),t=e[r.requestDate],i=y();let n=0;t!==i?(n=0,d().catch(()=>{})):n=e[r.dailyUsed]??0;const s=n+1;return await chrome.storage.local.set({[r.dailyUsed]:s,[r.requestDate]:i}),s}async function o(){const t=(await chrome.storage.local.get(["selectedProvider"])).selectedProvider||"free";return t==="free"?!1:!!(await chrome.storage.local.get([`${t}ApiKey`]))[`${t}ApiKey`]}async function c(){const e=await chrome.storage.local.get([r.isPro,r.userType]);return e[r.isPro]===!0||e[r.userType]===2}class T{async checkLimit(){if(await o())return{allowed:!0,remaining:1/0,limit:1/0};if(await c())return{allowed:!0,remaining:1/0,limit:1/0};const i=(await chrome.storage.local.get([r.dailyLimit]))[r.dailyLimit]||100,n=await u(),s=i-n;return s<=0?{allowed:!1,remaining:0,limit:i}:{allowed:!0,remaining:s,limit:i}}async incrementCount(){await o()||await c()||await I()}async getUsageStats(){if(await o())return{used:0,limit:1/0,remaining:1/0};if(await c())return{used:0,limit:1/0,remaining:1/0};const i=(await chrome.storage.local.get([r.dailyLimit]))[r.dailyLimit]||100,n=await u();return{used:n,limit:i,remaining:Math.max(0,i-n)}}}const L=new T;export{o as h,L as r,D as t};
