import{EVE_HEALTH_ROUTE_PATH,EVE_INFO_ROUTE_PATH}from"#protocol/routes.js";import{ClientError}from"#client/client-error.js";import{ClientSession}from"#client/session.js";import{createInitialSessionState}from"#client/session-utils.js";import{createClientUrl}from"#client/url.js";var Client=class{#e;#t;#n;#r;constructor(e){this.#n=e.host,this.#e=e.auth,this.#t=e.headers,this.#r=e.maxReconnectAttempts??3}async health(){let t=createClientUrl(this.#n,EVE_HEALTH_ROUTE_PATH),r=await this.#i(),i=await fetch(t,{headers:r});if(!i.ok){let e=await i.text();throw new ClientError(i.status,e)}return await i.json()}async info(){let e=createClientUrl(this.#n,EVE_INFO_ROUTE_PATH),r=await this.#i(),i=await fetch(e,{headers:r});if(!i.ok){let e=await i.text();throw new ClientError(i.status,e)}return await i.json()}async fetch(e,t={}){let n=createClientUrl(this.#n,e),r=await this.#i(headersInitToRecord(t.headers));return await fetch(n,{...t,headers:r})}session(e){let t;return t=typeof e==`string`?{continuationToken:e,streamIndex:0}:e||createInitialSessionState(),new ClientSession({host:this.#n,maxReconnectAttempts:this.#r,resolveHeaders:e=>this.#i(e)},t)}async#i(e){let t=new Headers,n=await resolveHeadersValue(this.#t);for(let[e,r]of Object.entries(n))t.set(e,r);if(e)for(let[n,r]of Object.entries(e))t.set(n,r);let r=await this.#a();return r&&t.set(`authorization`,r),t}async#a(){let e=this.#e;if(e){if(`bearer`in e){let t=(await resolveTokenValue(e.bearer)).trim();return t.length===0?void 0:`Bearer ${t}`}if(`basic`in e){let t=await resolveTokenValue(e.basic.password);return`Basic ${encodeBasicCredentials(e.basic.username,t)}`}}}};async function resolveTokenValue(e){return typeof e==`function`?e():e}async function resolveHeadersValue(e){return e===void 0?{}:typeof e==`function`?await e():e}function headersInitToRecord(e){return e===void 0?{}:Object.fromEntries(new Headers(e).entries())}function encodeBasicCredentials(e,t){let n=new TextEncoder().encode(`${e}:${t}`),r=Array.from(n,e=>String.fromCodePoint(e)).join(``);return btoa(r)}export{Client};