import { init_write_file_atomic, writeTextAtomic } from "../infra/write-file-atomic.js";
import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { mkdir, readFile } from "fs/promises";
import { join } from "path";
import { existsSync } from "fs";
//#region src/session/config-store.ts
/**
* Session Config Store
* 
* Manages session-level configuration persistence.
* Stores thinking level, reasoning visibility, verbose mode, and other
* session-specific settings that can be overridden via commands.
*/
init_write_file_atomic();
init_logger();
const log = createLogger("SessionConfigStore");
/**
* Session config store manager.
* Each session can have its own configuration that overrides agent defaults.
*/
var SessionConfigStore = class {
	configDir;
	/** @param agentHomeDir — `resolveAgentHomeDir(…)` (parent of `sessions/` transcript store) */
	constructor(agentHomeDir) {
		this.configDir = join(agentHomeDir, "sessions", "config");
	}
	/**
	* Initialize the config store
	*/
	async initialize() {
		await mkdir(this.configDir, { recursive: true });
		log.debug("Session config store initialized");
	}
	/**
	* Get the config file path for a session
	*/
	getConfigPath(sessionKey) {
		const safeKey = sessionKey.replace(/[^a-zA-Z0-9_-]/g, "_");
		return join(this.configDir, `${safeKey}.json`);
	}
	/**
	* Get config for a session
	*/
	async get(sessionKey) {
		const configPath = this.getConfigPath(sessionKey);
		if (!existsSync(configPath)) return null;
		try {
			const content = await readFile(configPath, "utf-8");
			return JSON.parse(content);
		} catch (error) {
			log.error({
				sessionKey,
				error
			}, "Failed to read session config");
			return null;
		}
	}
	/**
	* Set config for a session (full replacement)
	*/
	async set(sessionKey, config) {
		const configPath = this.getConfigPath(sessionKey);
		const configWithTimestamp = {
			...config,
			updatedAt: Date.now()
		};
		try {
			await writeTextAtomic(configPath, JSON.stringify(configWithTimestamp, null, 2));
			log.debug({ sessionKey }, "Session config saved");
		} catch (error) {
			log.error({
				sessionKey,
				error
			}, "Failed to save session config");
			throw error;
		}
	}
	/**
	* Update config for a session (partial update)
	*/
	async update(sessionKey, partial) {
		const updated = {
			...await this.get(sessionKey),
			...partial,
			updatedAt: Date.now()
		};
		await this.set(sessionKey, updated);
		return updated;
	}
	/**
	* Delete config for a session
	*/
	async delete(sessionKey) {
		const configPath = this.getConfigPath(sessionKey);
		if (existsSync(configPath)) try {
			const { unlink } = await import("fs/promises");
			await unlink(configPath);
			log.debug({ sessionKey }, "Session config deleted");
		} catch (error) {
			log.error({
				sessionKey,
				error
			}, "Failed to delete session config");
			throw error;
		}
	}
	/**
	* Check if config exists for a session
	*/
	async has(sessionKey) {
		return existsSync(this.getConfigPath(sessionKey));
	}
	/**
	* Get all session configs
	*/
	async getAll() {
		const { readdir } = await import("fs/promises");
		const configs = /* @__PURE__ */ new Map();
		try {
			const files = await readdir(this.configDir);
			for (const file of files) {
				if (!file.endsWith(".json")) continue;
				const sessionKey = file.replace(".json", "").replace(/_/g, "-");
				const config = await this.get(sessionKey);
				if (config) configs.set(sessionKey, config);
			}
		} catch (error) {
			log.error({ error }, "Failed to list session configs");
		}
		return configs;
	}
	/**
	* Clear all session configs
	*/
	async clear() {
		const { readdir, rm } = await import("fs/promises");
		try {
			const files = await readdir(this.configDir);
			for (const file of files) if (file.endsWith(".json")) await rm(join(this.configDir, file), { force: true });
			log.debug("All session configs cleared");
		} catch (error) {
			log.error({ error }, "Failed to clear session configs");
			throw error;
		}
	}
};
/**
* Resolve thinking level for a session.
* Returns session config if set, otherwise falls back to agent defaults.
*/
async function resolveThinkingLevel(sessionConfigStore, sessionKey, agentDefault) {
	const config = await sessionConfigStore.get(sessionKey);
	if (config?.thinkingLevel) return config.thinkingLevel;
	return agentDefault;
}
/**
* Resolve reasoning level for a session.
*/
async function resolveReasoningLevel(sessionConfigStore, sessionKey, agentDefault) {
	const config = await sessionConfigStore.get(sessionKey);
	if (config?.reasoningLevel) return config.reasoningLevel;
	return agentDefault;
}
/**
* Resolve verbose level for a session.
*/
async function resolveVerboseLevel(sessionConfigStore, sessionKey, agentDefault) {
	const config = await sessionConfigStore.get(sessionKey);
	if (config?.verboseLevel) return config.verboseLevel;
	return agentDefault;
}
//#endregion
export { SessionConfigStore, resolveReasoningLevel, resolveThinkingLevel, resolveVerboseLevel };

//# sourceMappingURL=config-store.js.map