import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { config as runtimeConfig } from "../../config.js";
import { AUTH_CONFIG_KEYS, KEY_TO_ENV, WRITABLE_CONFIG_KEYS, isSensitiveConfigKey as isSensitiveConfigKeyShared, } from "../../lib/configKeys.js";
export { KEY_TO_ENV, WRITABLE_CONFIG_KEYS };
export const CONFIG_FILE = runtimeConfig.storage.configFile;
export const CONFIG_DIR = runtimeConfig.storage.configDir;
export const AUTH_KEYS = AUTH_CONFIG_KEYS;
export function isAuthConfigKey(key) {
    return AUTH_CONFIG_KEYS.has(key);
}
export function isWritableConfigKey(key) {
    return WRITABLE_CONFIG_KEYS.has(key);
}
export function isSensitiveConfigKey(key) {
    return isSensitiveConfigKeyShared(key);
}
export function redactValue(key, value) {
    if (isSensitiveConfigKey(key))
        return value ? "<redacted>" : value;
    return value;
}
export function loadFileCfg() {
    if (!existsSync(CONFIG_FILE))
        return {};
    try {
        return JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
    }
    catch {
        return {};
    }
}
export function saveFileCfg(cfg) {
    mkdirSync(CONFIG_DIR, { recursive: true });
    writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2));
}
export function getNestedKey(obj, dotKey) {
    const parts = dotKey.split(".");
    let cur = obj;
    for (const p of parts) {
        if (cur == null || typeof cur !== "object")
            return undefined;
        cur = cur[p];
    }
    return cur;
}
export function setNestedKey(obj, dotKey, value) {
    const parts = dotKey.split(".");
    let cur = obj;
    for (let i = 0; i < parts.length - 1; i++) {
        const part = parts[i];
        const next = cur[part];
        if (next == null || typeof next !== "object" || Array.isArray(next))
            cur[part] = {};
        cur = cur[part];
    }
    cur[parts[parts.length - 1]] = value;
}
export function deleteNestedKey(obj, dotKey) {
    const parts = dotKey.split(".");
    let cur = obj;
    for (let i = 0; i < parts.length - 1; i++) {
        if (cur == null || typeof cur !== "object")
            return false;
        cur = cur[parts[i]];
    }
    if (cur == null || typeof cur !== "object")
        return false;
    const last = parts[parts.length - 1];
    if (!(last in cur))
        return false;
    delete cur[last];
    return true;
}
export function stripSets(value) {
    if (value instanceof Set)
        return [...value].map(stripSets);
    if (Array.isArray(value))
        return value.map(stripSets);
    if (value && typeof value === "object") {
        const result = {};
        for (const [key, nested] of Object.entries(value))
            result[key] = stripSets(nested);
        return result;
    }
    return value;
}
export function buildEffectiveConfig() {
    return stripSets(runtimeConfig);
}
export function parseConfigValue(rawValue) {
    try {
        return JSON.parse(rawValue);
    }
    catch {
        return rawValue;
    }
}
export function envOverrideForKey(key) {
    const envVar = KEY_TO_ENV[key];
    if (!envVar || process.env[envVar] === undefined)
        return null;
    return { envVar, value: String(process.env[envVar]) };
}
export function displayPath(p) {
    const home = process.env.HOME || process.env.USERPROFILE || "";
    return home && p.startsWith(home) ? p.replace(home, "~") : p;
}
export function restartNotice() {
    return "note: server must be restarted to pick up config changes (run `ima2 serve`)";
}
