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