import{i as e}from"./jsx-runtime-CutOxEx1.js";import{r as t}from"./appConfig-CDeG57c-.js";import{n,t as r}from"./proxyDefaults-BVuxpTwU.js";var i=e({defaultLlmInsightsSettings:()=>p,defaultLlmProxySettings:()=>m,getLlmInsightsSettings:()=>h,getLlmProxySettings:()=>_,setLlmInsightsSettings:()=>g,setLlmProxySettings:()=>v}),a=`llm_insights_settings_v1`,o=`llm_proxy_settings_v1`,s=`llm_web_`,c=`https://api2.bysjob.com/api/llm/chat`,l=(e,t=2048)=>String(e??``).replace(/[^\x20-\x7E]/g,``).replace(/[\r\n]+/g,``).slice(0,t).trim(),u=()=>{try{let e=String(globalThis?.location?.protocol||``);return e!==`chrome-extension:`&&e!==`moz-extension:`&&e!==`safari-extension:`}catch{return!0}},d=(e,t)=>new Promise(n=>{let r=!1,i=e=>{r||(r=!0,n(e))},a=()=>{try{let n=localStorage.getItem(s+e);if(n){let e=JSON.parse(n);i(e&&typeof e==`object`?e:t);return}}catch{}i(t)};try{let n=globalThis?.chrome?.storage?.local;if(n?.get){let r=setTimeout(()=>a(),450);n.get({[e]:t},n=>{clearTimeout(r);let a=n?.[e];i(a&&typeof a==`object`?a:t)});return}}catch{}a()}),f=(e,t)=>new Promise((n,r)=>{try{let i=globalThis?.chrome?.storage?.local;if(i?.set){i.set({[e]:t},()=>{let e=globalThis?.chrome?.runtime?.lastError;e?r(e):n()});return}}catch{}try{localStorage.setItem(s+e,JSON.stringify(t))}catch{}n()}),p={jdMode:`excerpt`},m={enabled:!1,proxyUrl:``,gatewayApiKey:``,tier:`free`,clientId:``,userDisabled:!1},h=async()=>{let e=await d(a,p);return{jdMode:e?.jdMode===`full`||e?.jdMode===`none`||e?.jdMode===`excerpt`?e.jdMode:`excerpt`}},g=async e=>{await f(a,{jdMode:e.jdMode===`full`||e.jdMode===`none`||e.jdMode===`excerpt`?e.jdMode:`excerpt`}),t({type:`SETTINGS_UPDATED`})},_=async()=>{let e=await new Promise(e=>{let t=!1,n=n=>{t||(t=!0,e(n))},r=()=>{try{let e=localStorage.getItem(s+o);n(e?JSON.parse(e):void 0)}catch{n(void 0)}};try{let e=globalThis?.chrome?.storage?.local;if(e?.get){let t=setTimeout(()=>r(),450);e.get([o],e=>{clearTimeout(t),n(e?.[o])});return}}catch{}r()}),t=e=>({enabled:!!e?.enabled,proxyUrl:String(e?.proxyUrl||``),gatewayApiKey:l(e?.gatewayApiKey||``,2048),tier:e?.tier===`pro`||e?.tier===`free`?e.tier:`free`,clientId:l(e?.clientId||``,120),userDisabled:!!e?.userDisabled}),i=e=>{let t=String(e.hostname||``).toLowerCase(),n=String(e.pathname||``).toLowerCase();return t.endsWith(`.supabase.co`)||n.includes(`/functions/v1/llm-proxy`)},a=(e,t=c)=>{let n=String(e||``).trim();if(!n)return``;let r=String(t||c).trim()||c;try{if(u()&&globalThis?.location?.origin){let e=String(globalThis.location.origin).replace(/\/+$/g,``),t=(()=>{try{return new URL(e).hostname.toLowerCase()}catch{return``}})(),a=t===`localhost`||t===`127.0.0.1`||t===`::1`||t===`0.0.0.0`;if(n.startsWith(`/`))return!a&&/^\/api\/llm(?:\/|$)/i.test(n)?c:new URL(n,e).toString();let o=new URL(n),s=String(o.hostname||``).toLowerCase();if(i(o))return r;if(!a&&(s===`bysjob.com`||s===`www.bysjob.com`)&&String(o.pathname||``).startsWith(`/api/llm`))return c}}catch{if(/supabase\.co\/functions\/v1\//i.test(n)||/\/functions\/v1\/llm-proxy/i.test(n))return r}try{return i(new URL(n))?r:n}catch{return n}};if(e&&typeof e==`object`){let o=t(e),s=n(),l=String(e?.gatewayApiKey||``)!==o.gatewayApiKey||String(e?.clientId||``)!==o.clientId,d=(()=>{try{return i(new URL(String(o.proxyUrl||``).trim()))}catch{return/supabase\.co\/functions\/v1\//i.test(String(o.proxyUrl||``))||/\/functions\/v1\/llm-proxy/i.test(String(o.proxyUrl||``))}})(),f=a(o.proxyUrl,s.proxyUrl||c);if(f&&f!==o.proxyUrl)return o.proxyUrl=f,d&&(o.gatewayApiKey=s.gatewayApiKey,o.userDisabled=!1),o.userDisabled||(o.enabled=!0),await v(o),o;if(l)return await v(o),o;let p=!!s.proxyUrl,m=!o.proxyUrl;if(!o.userDisabled&&p&&(m||!o.enabled)){let e={enabled:!0,proxyUrl:a(s.proxyUrl),gatewayApiKey:s.gatewayApiKey,tier:o.tier||`free`,clientId:o.clientId||r(),userDisabled:!1};return await v(e),e}if(u()&&p&&(o.userDisabled||!o.enabled||m)){let e={enabled:!0,proxyUrl:a(s.proxyUrl),gatewayApiKey:s.gatewayApiKey||o.gatewayApiKey,tier:o.tier||`free`,clientId:o.clientId||r(),userDisabled:!1};return await v(e),e}return o.clientId||(o.clientId=r(),await v(o)),o}let d=n(),f={enabled:!!d.proxyUrl,proxyUrl:d.proxyUrl||``,gatewayApiKey:d.gatewayApiKey||``,tier:`free`,clientId:r(),userDisabled:!1};return f.enabled&&await v(f),f},v=async e=>{await f(o,{enabled:!!e.enabled,proxyUrl:String(e.proxyUrl||``).trim(),gatewayApiKey:l(e.gatewayApiKey||``,2048),tier:e.tier===`pro`||e.tier===`free`?e.tier:`free`,clientId:l(e.clientId||``,120),userDisabled:!!e.userDisabled}),t({type:`SETTINGS_UPDATED`})};export{v as i,_ as n,i as r,h as t};