import{a as e,i as t,n,r,s as i,t as a}from"./security-CTzQb9Ko.js";var o=i({APIError:()=>l,TokenManager:()=>c,apiClient:()=>u}),s=class{static logRequest(e,n,i){t.debug.apiLogging&&(console.group(`[API] ${e.toUpperCase()} ${n}`),i&&console.log(`Request Data:`,r(i)),console.groupEnd())}static logResponse(e,n,r){if(!t.debug.apiLogging)return;let i=e>=200&&e<300?`color: green`:`color: red`;console.log(`%c[API] ${e} ${n}`,i),t.debug.verbose&&r&&console.log(`Response Data:`,r)}static logError(e){console.error(`[API Error]`,{status:e.response?.status,url:e.config?.url,message:e.message,...t.app.isDevelopment&&{error:e}})}},c=class{static ACCESS_TOKEN_KEY=`oquis_auth_token`;static REFRESH_TOKEN_KEY=`oquis_refresh_token`;static getAccessToken(){return localStorage.getItem(this.ACCESS_TOKEN_KEY)}static setAccessToken(e,t=!1){localStorage.setItem(this.ACCESS_TOKEN_KEY,e),sessionStorage.removeItem(this.ACCESS_TOKEN_KEY)}static getRefreshToken(){return localStorage.getItem(this.REFRESH_TOKEN_KEY)}static setRefreshToken(e,t=!1){localStorage.setItem(this.REFRESH_TOKEN_KEY,e),sessionStorage.removeItem(this.REFRESH_TOKEN_KEY)}static clearTokens(){sessionStorage.removeItem(this.ACCESS_TOKEN_KEY),sessionStorage.removeItem(this.REFRESH_TOKEN_KEY),localStorage.removeItem(this.ACCESS_TOKEN_KEY),localStorage.removeItem(this.REFRESH_TOKEN_KEY),n(`token_cleared`,{reason:`manual_logout`})}static hasValidToken(){return!!this.getAccessToken()}static isRememberedSession(){return!!localStorage.getItem(this.ACCESS_TOKEN_KEY)}},l=class extends Error{statusCode;errors;userMessage;constructor(e,t,n){super(e),this.name=`APIError`,this.statusCode=t,this.errors=n,this.userMessage=a(this)}getFieldError(e){return this.errors?.[e]?.[0]}isRetriable(){return this.statusCode?[408,429,500,502,503,504].includes(this.statusCode):!0}},u=new class{client;isRefreshing=!1;failedQueue=[];constructor(){this.client=e.create({baseURL:t.api.baseUrl,timeout:t.api.timeout,headers:{"Content-Type":`application/json`}}),this.setupInterceptors()}setupInterceptors(){this.client.interceptors.request.use(e=>{let t=c.getAccessToken();return t&&(e.headers.Authorization=`Bearer ${t}`),e.data instanceof FormData&&delete e.headers[`Content-Type`],s.logRequest(e.method||`GET`,e.url||``,e.data),e},e=>(s.logError(e),Promise.reject(e))),this.client.interceptors.response.use(e=>(s.logResponse(e.status,e.config.url||``,e.data),e),async r=>{s.logError(r);let i=r.config;if(r.response?.status===401&&!i._retry){let a=i.url||``;if(a.includes(`/auth/login/`)||a.includes(`/auth/refresh/`))return Promise.reject(this.handleError(r));if(this.isRefreshing)return new Promise((e,t)=>{this.failedQueue.push({resolve:e,reject:t})}).then(()=>this.client(i)).catch(e=>Promise.reject(e));i._retry=!0,this.isRefreshing=!0;try{let n=c.getRefreshToken();if(!n)throw Error(`No refresh token available`);let{access:r}=(await e.post(`${t.api.baseUrl}/api/v1/auth/auth/refresh/`,{refresh:n})).data,a=c.isRememberedSession();return c.setAccessToken(r,a),this.failedQueue.forEach(e=>e.resolve()),this.failedQueue=[],this.client(i)}catch(e){return this.failedQueue.forEach(t=>t.reject(e)),this.failedQueue=[],c.clearTokens(),n(`token_refresh_failed`,{error:e instanceof Error?e.message:`Unknown error`}),window.location.pathname.includes(`/login`)||(window.location.href=`/login`),Promise.reject(e)}finally{this.isRefreshing=!1}}return Promise.reject(this.handleError(r))})}handleError(e){if(e.response){let t=e.response.data,n=t?.error,r=(typeof n==`object`?n?.message:null)||(typeof n==`string`?n:null)||t?.detail||`An error occurred`,i=(typeof n==`object`?n?.details:void 0)||t?.errors||void 0;return new l(r,e.response.status,i)}else if(e.request)return new l(`Unable to connect to the server. Please check your connection.`,void 0);else return new l(e.message||`Request setup failed`,void 0)}async get(e,t){return(await this.client.get(e,t)).data}async post(e,t,n){return(await this.client.post(e,t,n)).data}async put(e,t,n){return(await this.client.put(e,t,n)).data}async patch(e,t,n){return(await this.client.patch(e,t,n)).data}async delete(e,t){return(await this.client.delete(e,t)).data}async getWithHeaders(e,t){let n=await this.client.get(e,t);return{data:n.data,headers:n.headers}}async upload(e,t,n){let r=new FormData;return r.append(`file`,t),this.post(e,r,{headers:{"Content-Type":`multipart/form-data`},onUploadProgress:e=>{n&&e.total&&n(Math.round(e.loaded*100/e.total))}})}async download(e,t){let n=await this.client.get(e,{responseType:`blob`}),r=new Blob([n.data]),i=document.createElement(`a`);i.href=window.URL.createObjectURL(r),i.download=t,i.click(),window.URL.revokeObjectURL(i.href)}};export{u as n,o as r,c as t};