//#region src/services/api.ts
var e = new Set([
	"POST",
	"PUT",
	"DELETE",
	"PATCH"
]), t = class extends Error {
	status;
	body;
	constructor(e, t, n) {
		super(t), this.name = "ApiError", this.status = e, this.body = n;
	}
};
function n(e, t) {
	return `${e.endsWith("/") ? e.slice(0, -1) : e}${t.startsWith("/") ? t : `/${t}`}`;
}
var r = class {
	config;
	constructor(e) {
		this.config = e;
	}
	async get(e, t) {
		return this.request(e, {
			...t,
			method: "GET"
		});
	}
	async post(e, t, n) {
		let r = { "Content-Type": "application/json" };
		if (n?.headers) {
			let e = n.headers;
			for (let t of Object.keys(e)) {
				let n = e[t];
				n !== void 0 && (r[t] = n);
			}
		}
		let i = {
			method: "POST",
			headers: r
		};
		return t !== void 0 && (i.body = JSON.stringify(t)), this.request(e, i);
	}
	async put(e, t, n) {
		let r = { "Content-Type": "application/json" };
		if (n?.headers) {
			let e = n.headers;
			for (let t of Object.keys(e)) {
				let n = e[t];
				n !== void 0 && (r[t] = n);
			}
		}
		let i = {
			method: "PUT",
			headers: r
		};
		return t !== void 0 && (i.body = JSON.stringify(t)), this.request(e, i);
	}
	async delete(e, t) {
		return this.request(e, {
			...t,
			method: "DELETE"
		});
	}
	async request(r, i) {
		let a = n(this.config.baseUrl ?? "", r), o = { ...this.config.headers };
		if (this.config.getAuthToken) {
			let e = this.config.getAuthToken();
			e && (o.Authorization = `Bearer ${e}`);
		}
		let s = (i?.method ?? "GET").toUpperCase();
		if (this.config.getCsrfToken && e.has(s)) {
			let e = this.config.getCsrfToken();
			e && (o[this.config.csrfHeaderName ?? "X-CSRF-Token"] = e);
		}
		let c = { ...o }, l = i ?? {};
		if (l.headers) {
			let e = l.headers;
			for (let t of Object.keys(e)) {
				let n = e[t];
				n !== void 0 && (c[t] = n);
			}
		}
		let u = await fetch(a, {
			...l,
			headers: c
		});
		if (u.status === 401 && this.config.onUnauthorized && this.config.onUnauthorized(), !u.ok) {
			let e;
			try {
				e = await u.json();
			} catch {}
			let n = `Request failed with status ${String(u.status)}`;
			if (typeof e == "object" && e && "message" in e) {
				let t = e.message;
				typeof t == "string" && (n = t);
			}
			throw new t(u.status, n, e);
		}
		return await u.json();
	}
};
function i(e) {
	return new r(e ?? {});
}
//#endregion
export { r as ApiClient, i as createApiClient };

//# sourceMappingURL=api.js.map