import { resolveStateDir } from "../config/paths-state.js";
import { init_paths } from "../config/paths.js";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
//#region src/tui/tui-settings.ts
init_paths();
const DEFAULT_TUI_SETTINGS = {
	theme: "auto",
	showThinking: false,
	toolsExpanded: false,
	doubleEscapeAction: "none",
	showTerminalProgress: false,
	showStartupHints: true
};
const SETTINGS_PATH = join(resolveStateDir(), "tui-settings.json");
function isDoubleEscapeAction(value) {
	return value === "none" || value === "tree" || value === "fork";
}
function normalizeSettings(raw) {
	const base = { ...DEFAULT_TUI_SETTINGS };
	if (!raw || typeof raw !== "object") return base;
	const obj = raw;
	if (typeof obj.theme === "string" && obj.theme.trim()) base.theme = obj.theme.trim();
	if (typeof obj.showThinking === "boolean") base.showThinking = obj.showThinking;
	if (typeof obj.toolsExpanded === "boolean") base.toolsExpanded = obj.toolsExpanded;
	if (isDoubleEscapeAction(obj.doubleEscapeAction)) base.doubleEscapeAction = obj.doubleEscapeAction;
	if (typeof obj.showTerminalProgress === "boolean") base.showTerminalProgress = obj.showTerminalProgress;
	if (typeof obj.showStartupHints === "boolean") base.showStartupHints = obj.showStartupHints;
	return base;
}
/** Load persisted TUI settings from `~/.xopc/tui-settings.json`. */
function loadTuiSettings() {
	try {
		if (!existsSync(SETTINGS_PATH)) return { ...DEFAULT_TUI_SETTINGS };
		const raw = readFileSync(SETTINGS_PATH, "utf8");
		return normalizeSettings(JSON.parse(raw));
	} catch {
		return { ...DEFAULT_TUI_SETTINGS };
	}
}
/** Persist TUI settings. */
function saveTuiSettings(settings) {
	const dir = resolveStateDir();
	if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
	writeFileSync(SETTINGS_PATH, `${JSON.stringify(settings, null, 2)}\n`, "utf8");
}
function getTuiSettingsPath() {
	return SETTINGS_PATH;
}
//#endregion
export { DEFAULT_TUI_SETTINGS, getTuiSettingsPath, loadTuiSettings, saveTuiSettings };

//# sourceMappingURL=tui-settings.js.map