const require_chunk = require("./chunk-DakpK96I.cjs");
const require_session_store = require("./session-store-yfwnj0OC.cjs");
let commander = require("commander");
let path = require("path");
path = require_chunk.__toESM(path, 1);
let fs = require("fs");
fs = require_chunk.__toESM(fs, 1);
//#region src/commands/session.ts
function sessionsDir(dataDir) {
	return path.default.join(dataDir, ".agentic", "sessions");
}
function persistSession(dataDir, session) {
	const dir = sessionsDir(dataDir);
	fs.default.mkdirSync(dir, { recursive: true });
	const key = (session.owner ?? `pid-${process.pid}`).replace(/[^a-zA-Z0-9_-]/g, "_");
	fs.default.writeFileSync(path.default.join(dir, `${key}.json`), JSON.stringify({
		...session,
		pid: process.pid
	}));
}
function clearPersistedSession(dataDir, owner) {
	const dir = sessionsDir(dataDir);
	if (!fs.default.existsSync(dir)) return;
	if (owner !== void 0) {
		const key = owner.replace(/[^a-zA-Z0-9_-]/g, "_");
		const file = path.default.join(dir, `${key}.json`);
		if (fs.default.existsSync(file)) fs.default.unlinkSync(file);
		return;
	}
	try {
		for (const f of fs.default.readdirSync(dir).filter((n) => n.endsWith(".json"))) try {
			fs.default.unlinkSync(path.default.join(dir, f));
		} catch {}
	} catch {}
}
function readAllSessions(dataDir) {
	const dir = sessionsDir(dataDir);
	if (!fs.default.existsSync(dir)) return [];
	return fs.default.readdirSync(dir).filter((f) => f.endsWith(".json")).map((f) => {
		try {
			return JSON.parse(fs.default.readFileSync(path.default.join(dir, f), "utf-8"));
		} catch {
			return null;
		}
	}).filter((s) => s !== null);
}
const sessionCommand = new commander.Command("session").description("Manage the active customer session");
sessionCommand.command("open <slug>").option("--owner <owner>", "Set the owner of this session").action(async (slug, opts) => {
	const dataDir = process.env["DXCRM_DATA_DIR"] ?? process.cwd();
	if (!require_session_store.customerExists(dataDir, slug)) {
		console.error(require_session_store.error(`✗ Customer not found: ${slug}`));
		process.exit(1);
	}
	const facts = await require_session_store.readMainFacts(dataDir, slug);
	const owner = opts.owner ?? process.env["DXCRM_ACTOR"];
	const session = {
		customerSlug: slug,
		customerName: facts.name,
		startedAt: (/* @__PURE__ */ new Date()).toISOString(),
		...owner !== void 0 ? { owner } : {}
	};
	require_session_store.setSession(session);
	persistSession(dataDir, session);
	console.log(require_session_store.success(`✓ Session opened: ${facts.name}`));
});
sessionCommand.command("close").action(() => {
	const dataDir = process.env["DXCRM_DATA_DIR"] ?? process.cwd();
	const s = require_session_store.getSession() ?? readAllSessions(dataDir)[0] ?? null;
	require_session_store.clearSession();
	clearPersistedSession(dataDir, s?.owner);
	console.log(require_session_store.success("✓ Session closed."));
});
sessionCommand.command("status").action(() => {
	const dataDir = process.env["DXCRM_DATA_DIR"] ?? process.cwd();
	const s = require_session_store.getSession() ?? readAllSessions(dataDir)[0] ?? null;
	if (!s) console.log(require_session_store.info("No active session."));
	else console.log(require_session_store.info(`Active: ${s.customerName} (${s.customerSlug}) since ${s.startedAt}`));
});
//#endregion
exports.readAllSessions = readAllSessions;

//# sourceMappingURL=session-Mm7GQbSH.cjs.map