"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfigPath = getConfigPath;
exports.loadAgentConfig = loadAgentConfig;
exports.saveAgentConfig = saveAgentConfig;
exports.updateAgentConfig = updateAgentConfig;
exports.setApiKey = setApiKey;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const node_os_1 = __importDefault(require("node:os"));
const DEFAULT_CONFIG = {
    ollama: {
        baseUrl: "http://localhost:11434",
        model: "llama3.1:8b",
    },
    github: {
        baseUrl: "https://models.inference.ai.azure.com",
        model: "openai/gpt-4o-mini",
    },
    gemini: {
        model: "gemini-2.0-flash",
    },
    openai: {
        model: "gpt-4o-mini",
    },
    agent: {
        maxIterations: 3,
        confirmDestructive: true,
        verbose: false,
    },
};
/**
 * Get the config file path: ~/.uso-agent.json
 */
function getConfigPath() {
    return (0, node_path_1.join)(node_os_1.default.homedir(), ".uso-agent.json");
}
/**
 * Load agent config from disk, merging with defaults.
 * Missing file = just use defaults.
 */
function loadAgentConfig() {
    const configPath = getConfigPath();
    try {
        const raw = (0, node_fs_1.readFileSync)(configPath, "utf8");
        const userConfig = JSON.parse(raw);
        return mergeConfig(userConfig);
    }
    catch {
        // File doesn't exist or is invalid — return defaults
        return { ...DEFAULT_CONFIG };
    }
}
/**
 * Save agent config to disk.
 */
function saveAgentConfig(config) {
    const configPath = getConfigPath();
    (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(configPath), { recursive: true });
    // Strip undefined values for cleaner JSON
    const clean = JSON.parse(JSON.stringify(config));
    (0, node_fs_1.writeFileSync)(configPath, JSON.stringify(clean, null, 2) + "\n", "utf8");
}
/**
 * Update specific fields in the config file.
 */
function updateAgentConfig(updates) {
    const current = loadAgentConfig();
    const merged = mergeConfig({ ...current, ...updates });
    saveAgentConfig(merged);
    return merged;
}
/**
 * Set a specific API key
 */
function setApiKey(provider, key) {
    const current = loadAgentConfig();
    if (provider === "gemini") {
        current.gemini = { ...current.gemini, apiKey: key };
    }
    else if (provider === "openai") {
        current.openai = { ...current.openai, apiKey: key };
    }
    else if (provider === "github") {
        current.github = { ...current.github, token: key };
    }
    saveAgentConfig(current);
    return current;
}
/**
 * Deep merge user config with defaults
 */
function mergeConfig(user) {
    return {
        ollama: { ...DEFAULT_CONFIG.ollama, ...user.ollama },
        gemini: { ...DEFAULT_CONFIG.gemini, ...user.gemini },
        openai: { ...DEFAULT_CONFIG.openai, ...user.openai },
        github: { ...DEFAULT_CONFIG.github, ...user.github },
        agent: { ...DEFAULT_CONFIG.agent, ...user.agent },
    };
}
//# sourceMappingURL=agent-config.js.map