import { resolveStateDir } from "../config/paths-state.js";
import { init_paths } from "../config/paths.js";
import { XOPC_TUI_KEYBINDINGS } from "./xopc-tui-keybindings.js";
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { KeybindingsManager } from "@earendil-works/pi-tui";
//#region src/tui/tui-keybindings-file.ts
init_paths();
function isRecord(value) {
	return typeof value === "object" && value !== null && !Array.isArray(value);
}
function toKeybindingsConfig(value) {
	if (!isRecord(value)) return {};
	const config = {};
	for (const [key, binding] of Object.entries(value)) {
		if (typeof binding === "string") {
			config[key] = binding;
			continue;
		}
		if (Array.isArray(binding) && binding.every((entry) => typeof entry === "string")) config[key] = binding;
	}
	return config;
}
function loadRawConfig(path) {
	if (!existsSync(path)) return void 0;
	try {
		const parsed = JSON.parse(readFileSync(path, "utf8"));
		return isRecord(parsed) ? parsed : void 0;
	} catch {
		return;
	}
}
function getTuiKeybindingsPath() {
	return join(resolveStateDir(), "keybindings.json");
}
function loadTuiKeybindingsConfig(path = getTuiKeybindingsPath()) {
	return toKeybindingsConfig(loadRawConfig(path));
}
var XopcKeybindingsManager = class extends KeybindingsManager {
	constructor(userBindings = {}, configPath = getTuiKeybindingsPath()) {
		super(XOPC_TUI_KEYBINDINGS, userBindings);
		this.configPath = configPath;
	}
	reload() {
		this.setUserBindings(loadTuiKeybindingsConfig(this.configPath));
	}
	getConfigPath() {
		return this.configPath;
	}
};
function createXopcTuiKeybindingsManager() {
	const configPath = getTuiKeybindingsPath();
	return new XopcKeybindingsManager(loadTuiKeybindingsConfig(configPath), configPath);
}
//#endregion
export { XopcKeybindingsManager, createXopcTuiKeybindingsManager, getTuiKeybindingsPath, loadTuiKeybindingsConfig };

//# sourceMappingURL=tui-keybindings-file.js.map