import { readFileSync, existsSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
let cachedVersion = null;
function findPackageJson() {
    let dir = fileURLToPath(new URL(".", import.meta.url));
    for (let i = 0; i < 5; i++) {
        const candidate = join(dir, "package.json");
        if (existsSync(candidate))
            return candidate;
        const parent = dirname(dir);
        if (parent === dir)
            break;
        dir = parent;
    }
    return null;
}
export function getKastellVersion() {
    if (cachedVersion !== null)
        return cachedVersion;
    try {
        const pkgPath = findPackageJson();
        if (!pkgPath)
            return "0.0.0";
        const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
        cachedVersion = pkg.version;
        return cachedVersion;
    }
    catch {
        return "0.0.0";
    }
}
export const KASTELL_VERSION = getKastellVersion();
export function clearVersionCache() {
    cachedVersion = null;
}
//# sourceMappingURL=version.js.map