import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
let cachedManifest = null;
function getPackageRoot() {
    return path.resolve(__dirname, "..");
}
export function getTemplatesDir() {
    const packageRoot = getPackageRoot();
    const candidates = [
        path.join(packageRoot, "templates"),
        path.join(packageRoot, "..", "templates"),
    ];
    for (const candidate of candidates) {
        if (fs.existsSync(candidate)) {
            return candidate;
        }
    }
    throw new Error("Could not find templates directory");
}
export function loadManifest() {
    if (cachedManifest) {
        return cachedManifest;
    }
    const packageRoot = getPackageRoot();
    const candidates = [
        path.join(packageRoot, "templates-manifest.json"),
        path.join(packageRoot, "..", "templates-manifest.json"),
    ];
    for (const candidate of candidates) {
        if (fs.existsSync(candidate)) {
            cachedManifest = JSON.parse(fs.readFileSync(candidate, "utf-8"));
            return cachedManifest;
        }
    }
    throw new Error("Could not find templates-manifest.json. Please run 'bun run build' first.");
}
export function getTemplates() {
    return loadManifest().templates;
}
export function getTemplateById(id) {
    return loadManifest().templates.find((template) => template.id === id);
}
//# sourceMappingURL=manifest.js.map