const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/tauri-auto-auth-Cqp6CoTW.js","assets/chunk-CMxvf4Kt.js","assets/preload-helper-zJ_50EbN.js","assets/api-base-B9gtivQY.js"])))=>i.map(i=>d[i]);
import{t as e}from"./preload-helper-zJ_50EbN.js";import{r as t,t as n}from"./api-base-B9gtivQY.js";var r=`unsloth:auth-session-cleared`,i=`unsloth:auth-session-stored`,a=`unsloth_auth_token`,o=`unsloth_auth_refresh_token`,s=`unsloth_auth_must_change_password`,c=0;function l(){return c}function u(){return typeof window<`u`}function d(){return u()?!!localStorage.getItem(a):!1}function f(){return u()?!!localStorage.getItem(o):!1}function p(){return u()?localStorage.getItem(a):null}function m(){return u()?localStorage.getItem(o):null}function h(e,t){if(!u())return;let n=!localStorage.getItem(a);localStorage.setItem(a,e),localStorage.setItem(o,t),n&&(c+=1,window.dispatchEvent(new Event(i)))}function g(){u()&&(c+=1,localStorage.removeItem(a),localStorage.removeItem(o),localStorage.removeItem(s),window.dispatchEvent(new Event(r)))}function _(){return u()?localStorage.getItem(s)!==null:!1}function v(e){u()&&(e?localStorage.setItem(s,`1`):localStorage.removeItem(s))}function y(){return t?`/chat`:_()?`/change-password`:`/chat`}var b=!1,x=null,S=null,C=0,w=[250,750,1500];function T(e){return new Promise(t=>setTimeout(t,e))}function E(e){(!e||m()===e)&&g()}async function D(e,n,r=!0){for(let i=0;;i++)try{return await fetch(e,n)}catch(e){if(!t||!r||!(e instanceof TypeError)||i>=w.length)throw e;await T(w[i])}}async function O(e){if(e.status!==403)return!1;try{return(await e.clone().json()).detail===`Password change required`}catch{return!1}}async function k(){if(b)return;b=!0;let e=`/login`;try{let t=await fetch(n(`/api/auth/status`));if(t.ok){let n=await t.json();n.requires_password_change!==_()&&v(n.requires_password_change),n.requires_password_change&&(e=`/change-password`)}}catch{}if(window.location.pathname===e){b=!1;return}window.location.href=e}function A(e){return e instanceof TypeError?!t&&typeof navigator<`u`&&navigator.onLine===!1?Object.assign(Error(`You appear to be offline. Check your network connection and try again.`),{unslothTransportFailure:!0}):Object.assign(Error(`Unsloth isn't running -- please relaunch it.`),{unslothTransportFailure:!0}):e}async function j(e,t,n=!0){let r=new Headers(t?.headers),i=p();i&&r.set(`Authorization`,`Bearer ${i}`);try{return await D(e,{...t,headers:r},n)}catch(e){throw A(e)}}async function M(t,n,r=!0){g();let{tauriAutoAuth:i}=await e(async()=>{let{tauriAutoAuth:e}=await import(`./tauri-auto-auth-Cqp6CoTW.js`).then(e=>e.i);return{tauriAutoAuth:e}},__vite__mapDeps([0,1,2,3]));return await i()?j(t,n,r):null}async function N(){let e=m();if(!e)return!1;if(x&&S===e)return x;let t=C,r=(async()=>{try{let r=await D(n(`/api/auth/refresh`),{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({refresh_token:e})});if(!r.ok)return E(e),!1;let i=await r.json();return t!==C||m()!==e?!1:(h(i.access_token,i.refresh_token),v(i.must_change_password??!1),!0)}catch{return!1}})();x=r,S=e;try{return await r}finally{x===r&&(x=null,S=null)}}async function P(e,r,i){let a=typeof e==`string`?n(e):e,o=new Headers(r?.headers),s=p();s&&o.set(`Authorization`,`Bearer ${s}`);let c;try{c=await D(a,{...r,headers:o},i?.retryNetworkErrors??!0)}catch(e){throw A(e)}if(await O(c))return t?await M(a,r,i?.retryNetworkErrors??!0)??c:(k(),c);if(c.status!==401)return c;let l=m();return await N()?_()?t?await M(a,r,i?.retryNetworkErrors??!0)??c:(k(),c):(p()||g(),j(a,r,i?.retryNetworkErrors??!0)):t?await M(a,r,i?.retryNetworkErrors??!0)??c:(E(l),k(),c)}async function F(e){try{return await D(n(`/api/auth/logout`),{method:`POST`,headers:e?{Authorization:`Bearer ${e}`}:void 0})}catch{return null}}async function I(){try{let e=await F(p());e&&e.status===401&&m()&&await N()&&(e=await F(p()))}finally{C+=1,g()}}export{l as a,d as c,v as d,h as f,g as i,f as l,i as m,I as n,p as o,r as p,N as r,y as s,P as t,_ as u};