import fs from "node:fs";
import path from "node:path";
import { resolveDefaultConfigPath } from "./home-paths.js";
const ZMEEL_CONFIG_BASENAME = "config.json";
const ZMEEL_ENV_FILENAME = ".env";
function findConfigFileFromAncestors(startDir) {
    const absoluteStartDir = path.resolve(startDir);
    let currentDir = absoluteStartDir;
    while (true) {
        const candidate = path.resolve(currentDir, ".zmeel", ZMEEL_CONFIG_BASENAME);
        if (fs.existsSync(candidate)) {
            return candidate;
        }
        const nextDir = path.resolve(currentDir, "..");
        if (nextDir === currentDir)
            break;
        currentDir = nextDir;
    }
    return null;
}
export function resolvezmeelConfigPath(overridePath) {
    if (overridePath)
        return path.resolve(overridePath);
    if (process.env.ZMEEL_CONFIG)
        return path.resolve(process.env.ZMEEL_CONFIG);
    return findConfigFileFromAncestors(process.cwd()) ?? resolveDefaultConfigPath();
}
export function resolvezmeelEnvPath(overrideConfigPath) {
    return path.resolve(path.dirname(resolvezmeelConfigPath(overrideConfigPath)), ZMEEL_ENV_FILENAME);
}
//# sourceMappingURL=paths.js.map