import { existsSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { OPENCODE_CONFIG_BASENAME, SEED_CONFIG_BASENAME } from "./constants.js";
const CONFIG_EXTENSIONS = [".jsonc", ".json"];
export function userOpenCodeConfigDir(configHome = defaultOpenCodeConfigHome()) {
    return configHome;
}
export function defaultOpenCodeConfigHome() {
    return process.env.OPENCODE_CONFIG_DIR || path.join(os.homedir(), ".config", "opencode");
}
export function resolveOpenCodeConfigPath(options = {}) {
    return firstExistingConfigPath(userOpenCodeConfigDir(options.configHome), OPENCODE_CONFIG_BASENAME);
}
export function resolveUserSeedConfigPath(options = {}) {
    return firstExistingConfigPath(userOpenCodeConfigDir(options.configHome), SEED_CONFIG_BASENAME);
}
export function projectSeedConfigPath(cwd = process.cwd()) {
    return path.join(path.resolve(cwd), ".opencode", `${SEED_CONFIG_BASENAME}.jsonc`);
}
export function projectOpenCodeConfigPath(cwd = process.cwd()) {
    return path.join(path.resolve(cwd), ".opencode", `${OPENCODE_CONFIG_BASENAME}.jsonc`);
}
export function projectRootOpenCodeConfigPath(cwd = process.cwd()) {
    return path.join(path.resolve(cwd), `${OPENCODE_CONFIG_BASENAME}.jsonc`);
}
export function packageRootFromCli() {
    return path.resolve(fileURLToPath(new URL("../../..", import.meta.url)));
}
export function packageRootPluginSpec(root = packageRootFromCli()) {
    return pathToFileURL(root).href;
}
function firstExistingConfigPath(dir, basename) {
    for (const extension of CONFIG_EXTENSIONS) {
        const file = path.join(dir, `${basename}${extension}`);
        if (existsSync(file))
            return file;
    }
    return path.join(dir, `${basename}.jsonc`);
}
//# sourceMappingURL=paths.js.map