import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { homedir } from "os";
import { dirname, join } from "path";
export function getUserConfigPath() {
    return join(homedir(), ".d3k", "config.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 };
            }
        }
        return config;
    }
    catch {
        return {};
    }
}
export function saveUserConfig(updates) {
    const configPath = getUserConfigPath();
    const configDir = dirname(configPath);
    // Ensure directory exists
    if (!existsSync(configDir)) {
        mkdirSync(configDir, { recursive: true });
    }
    // 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;
    }
    writeFileSync(configPath, JSON.stringify(merged, null, 2));
}
//# sourceMappingURL=user-config.js.map