import path from "path";
import fs from "fs";
//#region src/fs/audit-log.ts
const AUDIT_LOG_PATH = ".agentic/audit.log";
function getActor() {
	const actor = process.env["DXCRM_ACTOR"];
	return actor && actor.trim().length > 0 ? actor.trim() : "system";
}
function writeAuditEntry(dataDir, entry) {
	const logPath = path.join(dataDir, AUDIT_LOG_PATH);
	const logDir = path.dirname(logPath);
	if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true });
	const truncatedSummary = entry.summary.slice(0, 120);
	const line = `${entry.timestamp} | ${entry.actor} | ${entry.tool} | ${entry.slug} | ${truncatedSummary}\n`;
	fs.appendFileSync(logPath, line, "utf-8");
}
function readAuditLog(dataDir) {
	const logPath = path.join(dataDir, AUDIT_LOG_PATH);
	if (!fs.existsSync(logPath)) return [];
	const lines = fs.readFileSync(logPath, "utf-8").split("\n");
	const entries = [];
	for (const line of lines) {
		const trimmed = line.trim();
		if (!trimmed) continue;
		const parts = trimmed.split(" | ");
		if (parts.length < 5) continue;
		const [timestamp, actor, tool, slug, ...summaryParts] = parts;
		const summary = summaryParts.join(" | ");
		if (timestamp && actor && tool && slug) entries.push({
			timestamp,
			actor,
			tool,
			slug,
			summary: summary ?? ""
		});
	}
	return entries;
}
function filterAuditLog(entries, opts) {
	let filtered = entries;
	if (opts.slug !== void 0) filtered = filtered.filter((e) => e.slug === opts.slug);
	if (opts.actor !== void 0) filtered = filtered.filter((e) => e.actor === opts.actor);
	if (opts.limit !== void 0) filtered = filtered.slice(-opts.limit);
	return filtered;
}
//#endregion
export { writeAuditEntry as i, getActor as n, readAuditLog as r, filterAuditLog as t };

//# sourceMappingURL=audit-log-DNMY9mUZ.js.map