import { __esmMin } from "../../../_virtual/_rolldown/runtime.js";
//#region src/utils/logger/exporters.ts
function initializeExporters(configs) {
	for (const cfg of configs) switch (cfg.type) {
		case "loki":
			exporters.push(new LokiExporter(cfg));
			break;
		case "elk":
			exporters.push(new ElkExporter(cfg));
			break;
		case "datadog":
			exporters.push(new DatadogExporter(cfg));
			break;
		case "webhook":
			exporters.push(new WebhookExporter(cfg));
			break;
	}
}
async function exportLog(entry) {
	for (const exporter of exporters) if (exporter.enabled) try {
		await exporter.export(entry);
	} catch (err) {
		console.error(`[Exporter:${exporter.name}] Failed to export:`, err);
	}
}
async function flushExporters() {
	await Promise.all(exporters.map((e) => e.flush()));
}
function getExporters() {
	return [...exporters];
}
var LokiExporter, ElkExporter, DatadogExporter, WebhookExporter, exporters;
var init_exporters = __esmMin((() => {
	LokiExporter = class {
		name = "loki";
		enabled = true;
		url;
		labels;
		batch = [];
		batchSize;
		batchTimeoutMs;
		flushTimer = null;
		constructor(cfg) {
			this.url = cfg.url;
			this.labels = cfg.labels || { service: "xopc" };
			this.batchSize = cfg.batchSize || 100;
			this.batchTimeoutMs = cfg.batchTimeoutMs || 5e3;
		}
		async export(entry) {
			this.batch.push(entry);
			if (this.batch.length >= this.batchSize) await this.flush();
			else if (!this.flushTimer) this.flushTimer = setTimeout(() => this.flush(), this.batchTimeoutMs);
		}
		async flush() {
			if (this.batch.length === 0) return;
			if (this.flushTimer) {
				clearTimeout(this.flushTimer);
				this.flushTimer = null;
			}
			const payload = { streams: this.batch.map((entry) => ({
				stream: {
					...this.labels,
					level: entry.level
				},
				values: [[String(new Date(entry.timestamp).getTime() * 1e6), JSON.stringify(entry)]]
			})) };
			const batchToSend = this.batch;
			this.batch = [];
			try {
				const response = await fetch(`${this.url}/loki/api/v1/push`, {
					method: "POST",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify(payload)
				});
				if (!response.ok) {
					console.error(`[LokiExporter] Failed to send logs: ${response.status}`);
					this.batch.unshift(...batchToSend);
				}
			} catch (err) {
				console.error(`[LokiExporter] Error sending logs: ${err}`);
			}
		}
	};
	ElkExporter = class {
		name = "elk";
		enabled = true;
		url;
		index;
		apiKey;
		batch = [];
		batchSize;
		constructor(cfg) {
			this.url = cfg.url;
			this.index = cfg.index || "xopc-logs";
			this.apiKey = cfg.apiKey;
			this.batchSize = cfg.batchSize || 100;
		}
		async export(entry) {
			this.batch.push(entry);
			if (this.batch.length >= this.batchSize) await this.flush();
		}
		async flush() {
			if (this.batch.length === 0) return;
			const bulkBody = this.batch.flatMap((entry) => [JSON.stringify({ index: { _index: this.index } }), JSON.stringify(entry)]);
			const batchToSend = this.batch;
			this.batch = [];
			try {
				const headers = { "Content-Type": "application/json" };
				if (this.apiKey) headers["Authorization"] = `ApiKey ${this.apiKey}`;
				const response = await fetch(`${this.url}/_bulk`, {
					method: "POST",
					headers,
					body: bulkBody.join("\n") + "\n"
				});
				if (!response.ok) {
					console.error(`[ElkExporter] Failed to send logs: ${response.status}`);
					this.batch.unshift(...batchToSend);
				}
			} catch (err) {
				console.error(`[ElkExporter] Error sending logs: ${err}`);
			}
		}
	};
	DatadogExporter = class {
		name = "datadog";
		enabled = true;
		apiKey;
		site;
		service;
		batch = [];
		batchSize;
		constructor(cfg) {
			this.apiKey = cfg.apiKey;
			this.site = cfg.site || "datadoghq.com";
			this.service = cfg.service || "xopc";
			this.batchSize = cfg.batchSize || 100;
		}
		async export(entry) {
			this.batch.push({
				...entry,
				service: this.service
			});
			if (this.batch.length >= this.batchSize) await this.flush();
		}
		async flush() {
			if (this.batch.length === 0) return;
			const batchToSend = this.batch;
			this.batch = [];
			try {
				const response = await fetch(`https://http-intake.logs.${this.site}/v1/input`, {
					method: "POST",
					headers: {
						"Content-Type": "application/json",
						"DD-API-KEY": this.apiKey
					},
					body: JSON.stringify(batchToSend)
				});
				if (!response.ok) console.error(`[DatadogExporter] Failed to send logs: ${response.status}`);
			} catch (err) {
				console.error(`[DatadogExporter] Error sending logs: ${err}`);
			}
		}
	};
	WebhookExporter = class {
		name = "webhook";
		enabled = true;
		url;
		headers;
		filter;
		constructor(cfg) {
			this.url = cfg.url;
			this.headers = cfg.headers || { "Content-Type": "application/json" };
			this.filter = cfg.filter;
		}
		async export(entry) {
			if (this.filter && !this.filter.includes(entry.level)) return;
			try {
				await fetch(this.url, {
					method: "POST",
					headers: this.headers,
					body: JSON.stringify(entry)
				});
			} catch (err) {
				console.error(`[WebhookExporter] Error sending log: ${err}`);
			}
		}
		async flush() {}
	};
	exporters = [];
}));
//#endregion
init_exporters();
export { exportLog, flushExporters, getExporters, init_exporters, initializeExporters };

//# sourceMappingURL=exporters.js.map