import { __esmMin } from "../../../_virtual/_rolldown/runtime.js";
import { config, getLogDir, init_config } from "./config.js";
import path from "path";
import { createWriteStream, existsSync, mkdirSync } from "fs";
//#region src/utils/logger/streams.ts
/**
* Log Streams
* Output stream management (console, file, error file)
*/
/**
* Get log file path for a specific date and type
*/
function getLogPath(type = "app", date = /* @__PURE__ */ new Date()) {
	const dateStr = date.toISOString().split("T")[0];
	return path.join(getLogDir(), `${type}-${dateStr}.log`);
}
/**
* Ensure log directory exists
*/
function ensureLogDir() {
	if (!existsSync(config.logDir)) mkdirSync(config.logDir, { recursive: true });
}
/**
* Create log stream
*/
function createLogStream(filePath) {
	return createWriteStream(filePath, {
		flags: "a",
		encoding: "utf-8"
	});
}
/**
* Initialize and get all output streams
*/
function initializeStreams() {
	ensureLogDir();
	const streams = [];
	if (config.consoleOutput) streams.push({
		stream: process.stdout,
		level: config.level
	});
	if (config.fileOutput) {
		const appStream = createLogStream(getLogPath("app"));
		fileStreams.push(appStream);
		streams.push({
			stream: appStream,
			level: config.level
		});
	}
	if (config.errorFileOutput) {
		const errorStream = createLogStream(getLogPath("error"));
		fileStreams.push(errorStream);
		streams.push({
			stream: errorStream,
			level: "error"
		});
	}
	return streams;
}
/**
* Get all file streams for cleanup
*/
function getFileStreams() {
	return fileStreams;
}
/**
* Close all file streams gracefully
*/
async function closeStreams() {
	await Promise.all(fileStreams.map((stream) => new Promise((resolve) => {
		if (stream.writable) stream.end(() => resolve());
		else resolve();
	})));
	fileStreams.length = 0;
}
var fileStreams;
var init_streams = __esmMin((() => {
	init_config();
	fileStreams = [];
}));
//#endregion
init_streams();
export { closeStreams, getFileStreams, getLogPath, init_streams, initializeStreams };

//# sourceMappingURL=streams.js.map