/**
 * Install-runtime loader for bundled docs.
 *
 * Looks for the `docs-bundled/` mirror that `scripts/copy_docs_bundle.js`
 * placed alongside the compiled JS during `npm run build:server`. Falls back
 * to the repo's `docs/product/` source when running from `src/` (tests,
 * `tsx watch`, etc.) so the same module works in both layouts without an env
 * flag.
 */
import { existsSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { loadAllDocs, loadDocSource, listDocSlugs, } from "../docs_bundle/loader.js";
const CURRENT_FILE_DIR = path.dirname(fileURLToPath(import.meta.url));
/**
 * Resolve the bundled docs root. Order:
 *
 *   1. `NEOTOMA_DOCS_BUNDLE_DIR` env (escape hatch for tests/CI).
 *   2. `dist/docs-bundled/` next to this compiled file.
 *   3. `docs/product/` at the repo root (source-tree fallback).
 *
 * Returns null when none of the candidates exist (bundled docs feature is
 * effectively disabled at install).
 */
export function resolveDocsRootDir(env = process.env) {
    const explicit = (env.NEOTOMA_DOCS_BUNDLE_DIR || "").trim();
    if (explicit) {
        return existsSync(explicit) ? path.resolve(explicit) : null;
    }
    const distMirror = path.resolve(CURRENT_FILE_DIR, "..", "..", "docs-bundled");
    if (existsSync(distMirror))
        return distMirror;
    for (const candidate of [
        path.resolve(CURRENT_FILE_DIR, "..", "..", "..", "docs", "product"),
        path.resolve(CURRENT_FILE_DIR, "..", "..", "..", "..", "docs", "product"),
        path.resolve(process.cwd(), "docs", "product"),
    ]) {
        if (existsSync(candidate))
            return candidate;
    }
    return null;
}
export function listInstalledDocSlugs(env = process.env) {
    const root = resolveDocsRootDir(env);
    if (!root)
        return [];
    return listDocSlugs(root);
}
export function loadInstalledDoc(slug, env = process.env) {
    const root = resolveDocsRootDir(env);
    if (!root)
        return null;
    if (!listDocSlugs(root).includes(slug))
        return null;
    return loadDocSource(root, slug);
}
export function loadAllInstalledDocs(env = process.env) {
    const root = resolveDocsRootDir(env);
    if (!root)
        return [];
    return loadAllDocs(root);
}
//# sourceMappingURL=loader.js.map