import { i as writeTextAtomic } from "./json-files-kvH2Eboc.js";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { mkdir, readFile, readdir, stat } from "node:fs/promises";
//#region src/cognitive/evolution/types.ts
/** Default evolution configuration. */
const DEFAULT_EVOLUTION_CONFIG = { enabled: true };
//#endregion
//#region src/cognitive/evolution/store.ts
const COGNITIVE_DIR = "cognitive";
const EVOLUTION_DIR = "evolution";
const CONFIG_FILE = "config.json";
function createEvolutionDir(configDir, agentId) {
	if (agentId) return join(configDir, COGNITIVE_DIR, EVOLUTION_DIR, agentId);
	return join(configDir, COGNITIVE_DIR, EVOLUTION_DIR);
}
var EvolutionStore = class {
	constructor(configDir) {
		this.configDir = configDir;
	}
	recordPath(agentId, userId) {
		return join(createEvolutionDir(this.configDir, agentId), `${userId}.json`);
	}
	configPath(agentId) {
		return join(createEvolutionDir(this.configDir, agentId), CONFIG_FILE);
	}
	async save(agentId, record) {
		await mkdir(createEvolutionDir(this.configDir, agentId), { recursive: true });
		const targetPath = this.recordPath(agentId, record.userId);
		const records = await this.loadRecords(agentId, record.userId);
		const existingIdx = records.findIndex((r) => r.id === record.id);
		if (existingIdx >= 0) records[existingIdx] = record;
		else records.push(record);
		await writeTextAtomic(targetPath, JSON.stringify(records, null, 2));
	}
	async list(agentId, userId) {
		return this.loadRecords(agentId, userId);
	}
	async getRecentSuggestions(agentId, userId, hours) {
		const records = await this.loadRecords(agentId, userId);
		const cutoff = Date.now() - hours * 36e5;
		return records.filter((r) => r.timestamp > cutoff && r.draft !== void 0);
	}
	async loadConfig(agentId) {
		const path = this.configPath(agentId);
		if (!existsSync(path)) return { ...DEFAULT_EVOLUTION_CONFIG };
		const raw = await readFile(path, "utf-8");
		return {
			...DEFAULT_EVOLUTION_CONFIG,
			...JSON.parse(raw)
		};
	}
	async saveConfig(agentId, config) {
		await mkdir(createEvolutionDir(this.configDir, agentId), { recursive: true });
		await writeTextAtomic(this.configPath(agentId), JSON.stringify(config, null, 2));
	}
	async loadRecords(agentId, userId) {
		const path = this.recordPath(agentId, userId);
		if (!existsSync(path)) return [];
		try {
			const raw = await readFile(path, "utf-8");
			return JSON.parse(raw);
		} catch {
			return [];
		}
	}
	async listUserIds(agentId) {
		const dir = createEvolutionDir(this.configDir, agentId);
		try {
			return (await readdir(dir)).filter((name) => name.endsWith(".json") && name !== CONFIG_FILE).map((name) => name.slice(0, -5)).toSorted();
		} catch {
			return [];
		}
	}
	async listAgentIds() {
		const dir = createEvolutionDir(this.configDir);
		try {
			const entries = await readdir(dir);
			const result = [];
			for (const name of entries) if ((await stat(join(dir, name))).isDirectory()) result.push(name);
			return result.toSorted();
		} catch {
			return [];
		}
	}
};
//#endregion
export { EvolutionStore };
