var e=`4dp_logged_in`;function t(){return localStorage.getItem(e)===`1`}function n(){localStorage.setItem(e,`1`)}function r(){localStorage.removeItem(e)}async function i(e,t={}){let n=t.headers??{},i=Object.keys(n).length===0?{"Content-Type":`application/json`}:{...n};t.body instanceof FormData&&delete i[`Content-Type`];let a=await fetch(e,{...t,headers:i});if(a.status===401&&!e.includes(`/auth/`)){r();let e=encodeURIComponent(window.location.pathname+window.location.search);window.location.href=`/login?returnTo=${e}`}return a}var a={async get(e){let t=await i(e);if(!t.ok)throw Error(`${t.status}: ${t.statusText}`);return t.json()},async post(e,t){let n=await i(e,{method:`POST`,body:t?JSON.stringify(t):void 0});if(!n.ok){let e=n.statusText;try{e=(await n.json()).detail||e}catch{}throw Error(e)}return n.json()},async patch(e,t){let n=await i(e,{method:`PATCH`,body:JSON.stringify(t)});if(!n.ok){let e=n.statusText;try{e=(await n.json()).detail||e}catch{}throw Error(e)}return n.json()},async del(e){let t=await i(e,{method:`DELETE`});if(!t.ok)throw Error(`${t.status}: ${t.statusText}`)},async login(e,t){if(!(await fetch(`/api/v1/auth/login`,{method:`POST`,headers:{"Content-Type":`application/x-www-form-urlencoded`},body:new URLSearchParams({username:e,password:t}),credentials:`include`})).ok)throw Error(`Invalid credentials`);n()},async logout(){await fetch(`/api/v1/auth/logout`,{method:`POST`,credentials:`include`}),r()},async register(e,t,n,r){return this.post(`/api/v1/auth/register`,{email:e,username:r||e.split(`@`)[0],password:t,display_name:n})}};export{i as n,t as r,a as t};