import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { defaultSeedAgentConfig } from "./defaults.js";
import { mergeSeedConfig } from "./merge.js";
const PROJECT_CONFIG_NAMES = ["seed-agent.jsonc", "seed-agent.json"];
const USER_CONFIG_NAMES = ["seed-agent.jsonc", "seed-agent.json"];
export async function loadSeedConfig(directory, explicit) {
    const sources = [];
    const projectConfig = await loadFirstExisting(projectConfigPaths(directory));
    const ignoreUserConfig = shouldIgnoreUserConfig(projectConfig?.data, explicit);
    const userConfig = ignoreUserConfig ? undefined : await loadFirstExisting(userConfigPaths());
    if (userConfig)
        sources.push(userConfig.path);
    if (projectConfig)
        sources.push(projectConfig.path);
    return {
        config: mergeSeedConfig(userConfig?.data, projectConfig?.data, explicit),
        sources,
    };
}
function shouldIgnoreUserConfig(projectConfig, explicit) {
    if (explicit?.isolation?.ignore_user_config !== undefined)
        return explicit.isolation.ignore_user_config;
    if (!projectConfig)
        return false;
    return projectConfig.isolation?.ignore_user_config ?? defaultSeedAgentConfig.isolation.ignore_user_config;
}
function userConfigPaths() {
    return USER_CONFIG_NAMES.map((name) => path.join(os.homedir(), ".config", "opencode", name));
}
function projectConfigPaths(directory) {
    const paths = [];
    let current = path.resolve(directory);
    while (true) {
        for (const name of PROJECT_CONFIG_NAMES) {
            paths.push(path.join(current, ".opencode", name));
        }
        const parent = path.dirname(current);
        if (parent === current)
            break;
        current = parent;
    }
    return paths;
}
async function loadFirstExisting(paths) {
    for (const configPath of paths) {
        if (!existsSync(configPath))
            continue;
        const text = await readFile(configPath, "utf8");
        return { path: configPath, data: JSON.parse(stripJsonComments(text)) };
    }
    return undefined;
}
export function stripJsonComments(input) {
    let output = "";
    let inString = false;
    let quote = "";
    let escaped = false;
    for (let index = 0; index < input.length; index += 1) {
        const char = input[index];
        const next = input[index + 1];
        if (inString) {
            output += char;
            if (escaped) {
                escaped = false;
            }
            else if (char === "\\") {
                escaped = true;
            }
            else if (char === quote) {
                inString = false;
            }
            continue;
        }
        if (char === "\"" || char === "'") {
            inString = true;
            quote = char;
            output += char;
            continue;
        }
        if (char === "/" && next === "/") {
            while (index < input.length && input[index] !== "\n")
                index += 1;
            output += "\n";
            continue;
        }
        if (char === "/" && next === "*") {
            index += 2;
            while (index < input.length && !(input[index] === "*" && input[index + 1] === "/"))
                index += 1;
            index += 1;
            output += " ";
            continue;
        }
        output += char;
    }
    return output;
}
//# sourceMappingURL=loader.js.map