import { a as initializeAPI, i as getValidToken, n as ClientAblyService, r as apiRequest, t as axios } from "./axios-CRjNw1nT.js";
//#region src/core/ApiService.ts
var ApiService = class {
	axiosInstance;
	config;
	tokenExpiryTime = 0;
	currentToken = null;
	constructor(config) {
		this.config = config;
		this.axiosInstance = axios.create({
			baseURL: config.baseUrl,
			headers: { "Content-Type": "application/json" }
		});
		this.setupInterceptors();
	}
	setupInterceptors() {
		this.axiosInstance.interceptors.request.use(async (config) => {
			const token = await this.getValidToken();
			if (token && config.headers) config.headers.Authorization = `Bearer ${token}`;
			return config;
		}, (error) => Promise.reject(error));
		this.axiosInstance.interceptors.response.use((response) => response, async (error) => {
			if (error.response?.status === 401) {
				const token = await this.getValidToken(true);
				if (token && error.config) {
					error.config.headers.Authorization = `Bearer ${token}`;
					return this.axiosInstance.request(error.config);
				}
			}
			return Promise.reject(error);
		});
	}
	async getValidToken(forceRefresh = false) {
		const currentTime = Math.floor(Date.now() / 1e3);
		if (forceRefresh || !this.currentToken || currentTime >= this.tokenExpiryTime) try {
			const response = await this.config.getToken();
			if (!response || !response.token || !response.expiresIn) throw new Error("Invalid token response");
			this.currentToken = response.token;
			this.tokenExpiryTime = currentTime + response.expiresIn;
			return this.currentToken;
		} catch (error) {
			throw error;
		}
		return this.currentToken;
	}
	async get(endpoint, config) {
		try {
			return (await this.axiosInstance.get(endpoint, config)).data;
		} catch (error) {
			this.handleError(error);
			throw error;
		}
	}
	async post(endpoint, data, config) {
		try {
			return (await this.axiosInstance.post(endpoint, data, config)).data;
		} catch (error) {
			this.handleError(error);
			throw error;
		}
	}
	async put(endpoint, data, config) {
		try {
			return (await this.axiosInstance.put(endpoint, data, config)).data;
		} catch (error) {
			this.handleError(error);
			throw error;
		}
	}
	async delete(endpoint, config) {
		try {
			return (await this.axiosInstance.delete(endpoint, config)).data;
		} catch (error) {
			this.handleError(error);
			throw error;
		}
	}
	handleError(error) {
		if (this.config.onError) this.config.onError(error instanceof Error ? error : /* @__PURE__ */ new Error("Unknown error occurred"));
	}
};
//#endregion
//#region src/core/token-service.ts
var TokenService = class {
	baseUrl;
	constructor(baseUrl) {
		this.baseUrl = baseUrl;
	}
	async getToken() {
		try {
			const response = await fetch(`${this.baseUrl}/Auth/client/get-babylai-token`, {
				method: "POST",
				headers: { "Content-Type": "application/json" }
			});
			if (!response.ok) throw new Error("Failed to fetch token");
			const data = await response.json();
			return {
				token: data.token,
				expiresIn: data.expiresIn || 3600
			};
		} catch (error) {
			throw new Error("Failed to get authentication token");
		}
	}
};
//#endregion
export { ClientAblyService as AblyService, ApiService, TokenService, apiRequest, getValidToken, initializeAPI };

//# sourceMappingURL=services.js.map