import{a as e,c as t,d as n,i as r,o as i,u as a}from"./url-D6jvVYIA.js";var o=e((e,r)=>({baseUrl:typeof window<`u`?window.location.origin:``,token:void 0,tokenDialogOpen:!1,tokenExpired:!1,setGatewayToken:t=>{n(t),e({token:t,tokenDialogOpen:!1,tokenExpired:!1}),window.dispatchEvent(new CustomEvent(`token-saved`,{detail:{token:t}}))},clearGatewayToken:()=>{t(),e({token:void 0})},openTokenDialog:()=>e({tokenDialogOpen:!0}),closeTokenDialog:()=>e({tokenDialogOpen:!1}),onUnauthorized:()=>{r().clearGatewayToken(),e({tokenDialogOpen:!1,tokenExpired:!0}),window.dispatchEvent(new CustomEvent(`token-expired`))}}));function s(){let e=new URLSearchParams(window.location.search).get(`token`);if(e){o.getState().setGatewayToken(e);let t=window.location.pathname+window.location.hash,n=window.history.state,r=n&&typeof n==`object`&&!Array.isArray(n)?{...n}:n;window.history.replaceState(r,``,t);return}let t=a();o.setState({token:t||void 0,tokenDialogOpen:!1})}function c(){return r.getState().language}function l(e,t,n){let r=e.trim().toLowerCase();return!!(!r||r===`http ${t}`||n&&r===n.trim().toLowerCase()||t===500&&r===`internal server error`)}function u(e,t,n){let r=n?.trim()??``;if(r&&!l(r,e,t))return r;let a=i(c()).api;return e>=500?e===502?a.errorBadGateway:e===503?a.errorServiceUnavailable:e===504?a.errorGatewayTimeout:e===500?a.errorInternal:a.errorServer.replace(`{{status}}`,String(e)):e===404?a.errorNotFound:e===403?a.errorForbidden:a.errorRequest.replace(`{{status}}`,String(e))}var d=null,f=null;function p(){d||=new Promise(e=>{f=e})}function m(){d&&(f?.(),d=null,f=null)}typeof window<`u`&&window.addEventListener(`token-saved`,m);async function h(e,t){d&&await d;let n=o.getState().token,r=new Headers(t?.headers);r.has(`Content-Type`)||r.set(`Content-Type`,`application/json`),n&&r.set(`Authorization`,`Bearer ${n}`);let i=await fetch(e,{...t,headers:r});return i.status===401&&(p(),o.getState().onUnauthorized()),i}async function g(e,t){let n=await h(e,t);if(!n.ok){let e=await n.json().catch(()=>({})),t=typeof e.error==`string`?e.error:e.error?.message,r=u(n.status,n.statusText,t);throw Error(r)}return n.json()}export{o as a,s as i,g as n,u as r,h as t};