import{n as e}from"./chunk-B3pr0VN8.js";import{r as t,x as n}from"./authSession-CrP9lMPq.js";import{r}from"./norm-CeclA1Tu.js";var i=`hireedge_admin_console_session_v1`,a=30*1e3,o=e=>{try{return e?JSON.parse(e):null}catch{return null}},s=e=>{if(!e||typeof e!=`object`)return null;let t=r(e.accessToken||e.token),n=r(e.expiresAt||e.accessExpiresAt),i=e.user&&typeof e.user==`object`?e.user:{},a=r(i.id);return!t||!n||!a?null:{token:t,accessToken:t,refreshToken:r(e.refreshToken)||void 0,expiresAt:n,accessExpiresAt:r(e.accessExpiresAt||n)||n,refreshExpiresAt:r(e.refreshExpiresAt)||void 0,user:{id:a,phone:r(i.phone)||void 0,email:r(i.email)||void 0}}},c=e=>{if(!e)return!1;let t=Date.parse(e.expiresAt);return Number.isFinite(t)&&t>Date.now()+a},l=()=>{try{return String(sessionStorage.getItem(i)||localStorage.getItem(i)||``)}catch{try{return String(sessionStorage.getItem(i)||``)}catch{return``}}},u=()=>{let e=s(o(l()));return c(e)?e:(p(),null)},d=async()=>u(),f=async e=>{if(!e){try{sessionStorage.removeItem(i)}catch{}try{localStorage.removeItem(i)}catch{}return}let t=s(e);if(!t)throw Error(`invalid_admin_session`);let n=JSON.stringify(t),r=!1;try{sessionStorage.setItem(i,n),r=!0}catch{}try{localStorage.setItem(i,n),r=!0}catch{}if(!r)throw Error(`admin_session_storage_failed`)},p=async()=>{await f(null)},m=e({getAdminDashboard:()=>T,getLlmUsageEvents:()=>w,getLlmUsageMe:()=>C,getUsageApiConfig:()=>S}),h=`https://api2.bysjob.com`,g=()=>{let e=String(globalThis?.location?.protocol||``);return e===`chrome-extension:`||e===`moz-extension:`||e===`safari-extension:`},_=()=>{try{return`https://api2.bysjob.com`.replace(/\/+$/g,``)}catch{}if(!g()){let e=String(globalThis?.location?.origin||``).replace(/\/+$/g,``);if(e.includes(`localhost`)||e.includes(`127.0.0.1`))return e}return h},v=e=>`${_()}/api/usage${e.startsWith(`/`)?e:`/${e}`}`,y=async(e=`user`)=>{let i=e===`admin`?await d():await t(),a=r(i?.accessToken||i?.token);if(!a)throw Error(`请先登录`);let o={"Content-Type":`application/json`};o.Authorization=`Bearer ${a}`,o[`x-auth-token`]=a;try{let e=String(await n()||``).slice(0,64);e&&(o[`x-device-key`]=e)}catch{}return o},b=async(e,t,n,r)=>{let i=v(e),a=await y(r?.authSource||`user`),o=await fetch(i,{method:`POST`,headers:a,body:JSON.stringify(t||{})}),s=await o.text(),c=null;try{c=s?JSON.parse(s):null}catch{}if(!o.ok)throw Error(String(c?.message||c?.error||s||n));return c||{}},x=async(e,t,n)=>b(e,t,n,{authSource:`admin`}),S=async()=>({url:`${_()}/api/usage`,anonKey:``,clientId:``}),C=e=>{let t=Number(e?.rangeDays??30);return b(`/me`,{rangeDays:Number.isFinite(t)?Math.max(1,Math.min(90,Math.floor(t))):30,tier:e?.tier===`free`||e?.tier===`pro`?e.tier:`all`},`查询用量失败`)},w=e=>{let t=Number(e?.rangeDays??7),n=Number.isFinite(t)?Math.max(1,Math.min(90,Math.floor(t))):7,i=Number(e?.pageSize??50);return b(`/events`,{rangeDays:n,pageSize:Number.isFinite(i)?Math.max(1,Math.min(200,Math.floor(i))):50,cursor:r(e?.cursor||``)},`查询用量明细失败`)},T=e=>{let t=Number(e?.rangeDays??14);return x(`/admin_dashboard`,{rangeDays:Number.isFinite(t)?Math.max(1,Math.min(90,Math.floor(t))):14,includeDataCockpit:e?.includeDataCockpit!==!1},`查询管理员数据屏失败`)};export{m as n,w as t};