/**
 * Load the gate registry the report is derived from.
 *
 * Deliberately the running Lisa package's copy rather than the project's
 * `scripts/lisa-gates.mjs`, for the reason `doctor-skip-jobs-migration` already
 * gives: both are the same file in a healthy repository, but a stale project
 * copy answers with a table describing a workflow that is no longer there. It
 * matters twice over here — this repository's own `scripts/` holds neither
 * shipped script, so a report that read the project copy would report nothing
 * at all when run against Lisa itself.
 * @module cli/gate-report-registry
 */
import { existsSync } from "node:fs";
import * as path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/** Package-relative location of the shipped registry. */
const REGISTRY_RELATIVE = path.join("all", "copy-overwrite", "scripts", "lisa-gates.mjs");
/**
 * Walk parents until a package-root-relative file exists.
 * @param startDir - Directory to start searching from
 * @param relativePath - Path under the package root
 * @returns Absolute path, or null when no ancestor holds it
 */
function walkForPackageFile(startDir, relativePath) {
    const candidate = path.join(startDir, relativePath);
    if (existsSync(candidate))
        return candidate;
    const parent = path.dirname(startDir);
    return parent === startDir ? null : walkForPackageFile(parent, relativePath);
}
/**
 * Locate the shipped registry inside the running Lisa package.
 * @returns Absolute path, or null when it cannot be found
 */
export function resolveGateRegistryPath() {
    const fromPackageRoot = path.join(__dirname, "..", "..", REGISTRY_RELATIVE);
    if (existsSync(fromPackageRoot))
        return fromPackageRoot;
    return walkForPackageFile(__dirname, REGISTRY_RELATIVE);
}
/**
 * Locate the running Lisa package's root.
 *
 * Derived from the registry's own location rather than walked a second time.
 * Two walks would be two answers to "where is Lisa", and the one that went
 * stale would be the one nobody measured.
 * @returns Absolute package root, or null when the registry cannot be found
 */
export function resolveLisaPackageRoot() {
    const registry = resolveGateRegistryPath();
    if (registry === null)
        return null;
    // <root>/all/copy-overwrite/scripts/lisa-gates.mjs -> <root>
    return path.resolve(path.dirname(registry), "..", "..", "..");
}
/**
 * Import the shipped registry.
 * @returns The registry module, or null when it is not installed
 */
export async function loadGateRegistry() {
    const script = resolveGateRegistryPath();
    if (script === null)
        return null;
    return (await import(pathToFileURL(script).href));
}
//# sourceMappingURL=gate-report-registry.js.map