import { existsSync, readFileSync, writeFileSync } from "fs";
import { homedir } from "os";
import { join } from "path";
export function getUserConfigPath() {
    return join(homedir(), ".d3k.json");
}
function normalizeDisableList(value) {
    if (typeof value === "string" && value.trim().length > 0) {
        return value;
    }
    if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
        return value.join(" ");
    }
    return undefined;
}
export function loadUserConfig() {
    const configPath = getUserConfigPath();
    if (!existsSync(configPath)) {
        return {};
    }
    try {
        const content = readFileSync(configPath, "utf-8");
        const parsed = JSON.parse(content);
        const disableList = normalizeDisableList(parsed.disableMcpConfigs);
        const config = {};
        if (disableList) {
            config.disableMcpConfigs = disableList;
        }
        if (parsed.defaultAgent && typeof parsed.defaultAgent === "object") {
            const agent = parsed.defaultAgent;
            if (typeof agent.name === "string" && typeof agent.command === "string") {
                config.defaultAgent = { name: agent.name, command: agent.command };
            }
        }
        if (typeof parsed.browser === "string" && parsed.browser.trim().length > 0) {
            config.browser = parsed.browser.trim();
        }
        return config;
    }
    catch {
        return {};
    }
}
export function saveUserConfig(updates) {
    const configPath = getUserConfigPath();
    // Load existing config and merge
    const existing = loadUserConfig();
    const merged = { ...existing, ...updates };
    // Remove defaultAgent if set to undefined (user chose "No agent")
    if (updates.defaultAgent === undefined && "defaultAgent" in updates) {
        delete merged.defaultAgent;
    }
    // Remove browser if set to undefined
    if (updates.browser === undefined && "browser" in updates) {
        delete merged.browser;
    }
    writeFileSync(configPath, JSON.stringify(merged, null, 2));
}
//# sourceMappingURL=user-config.js.map