/**
 * Template Registry
 *
 * Defines available project templates for quickback create command.
 * The server is the source of truth for templates and tiers.
 * This local registry serves as a fallback when offline.
 */
import { fetchTemplates } from '../lib/api-client.js';
// Cache for server templates
let serverTemplatesCache = null;
/**
 * Fetch templates from server (with caching)
 */
export async function fetchServerTemplates() {
    if (serverTemplatesCache) {
        return serverTemplatesCache;
    }
    try {
        serverTemplatesCache = await fetchTemplates();
        return serverTemplatesCache;
    }
    catch {
        // Fallback to local registry
        return {};
    }
}
/**
 * Check if a template requires pro tier
 */
export async function isProTemplate(templateName) {
    const serverTemplates = await fetchServerTemplates();
    const resolved = TEMPLATE_ALIASES[templateName] || templateName;
    // Check server first
    if (serverTemplates[resolved]) {
        return serverTemplates[resolved].tier === 'pro';
    }
    // Fallback to local
    const local = TEMPLATE_REGISTRY[resolved];
    return local?.tier === 'pro';
}
export const TEMPLATE_REGISTRY = {
    // Free tier templates
    "betterauth-d1-cloudflare": {
        name: "Better Auth + D1 + Cloudflare",
        description: "Authentication with Better Auth, D1 database, deployed to Cloudflare Workers — bare scaffold, add your own features",
        category: "auth",
        tags: ["auth", "cloudflare", "d1", "hono"],
        author: "quickback",
        version: "1.0.0",
        includes: ["betterauth", "d1", "hono", "drizzle"],
        preset: "cloudflare",
        requiresCloudflare: true,
        tier: "free",
    },
    "todos-cloudflare": {
        name: "Todos Example (Cloudflare)",
        description: "Working todos example — schema with masking, two record-based actions (complete / uncomplete), Better Auth, D1. Read the source as a learning template.",
        category: "api",
        tags: ["example", "todos", "actions", "cloudflare", "d1"],
        author: "quickback",
        version: "1.0.0",
        includes: ["betterauth", "d1", "hono", "drizzle"],
        preset: "cloudflare",
        requiresCloudflare: true,
        tier: "free",
    },
    // bun template hidden — see apps/compiler/src/providers/runtime/providers/bun/README.md
    // → "Known Issues" for the list of generator drift bugs blocking it.
    // Pro tier templates
    "b2bsaas-cloudflare": {
        name: "B2B SaaS Starter",
        description: "Full B2B SaaS with auth, organizations, file storage (R2), webhooks, and views",
        category: "saas",
        tags: ["saas", "b2b", "cloudflare", "r2", "webhooks", "teams"],
        author: "quickback",
        version: "1.0.0",
        includes: ["betterauth", "organizations", "d1", "r2", "files", "webhooks", "hono", "drizzle"],
        preset: "cloudflare",
        requiresCloudflare: true,
        tier: "pro",
    },
    // api-minimal-bun also hidden alongside the other bun templates.
    "blog-cloudflare": {
        name: "Blog / CMS Starter",
        description: "Single-tenant blog with public pages, admin-only editing, deployed to Cloudflare Workers",
        category: "api",
        tags: ["blog", "cms", "single-tenant", "cloudflare", "d1"],
        author: "quickback",
        version: "1.0.0",
        includes: ["betterauth", "d1", "hono", "drizzle"],
        preset: "blog",
        requiresCloudflare: true,
        tier: "free",
    },
    "empty-cloudflare": {
        name: "Empty Scaffold (Cloudflare)",
        description: "Empty project structure with config only — no example features. Add your own definitions.",
        category: "api",
        tags: ["empty", "scaffold", "cloudflare", "d1"],
        author: "quickback",
        version: "1.0.0",
        includes: ["betterauth", "d1", "hono", "drizzle"],
        preset: "cloudflare",
        requiresCloudflare: true,
        tier: "free",
    },
    // empty-bun also hidden alongside the other bun templates.
};
// Aliases for common shortcuts
// Bun-targeting aliases (bun, local, minimal) are intentionally absent —
// see apps/compiler/src/providers/runtime/providers/bun/README.md for why.
export const TEMPLATE_ALIASES = {
    "cloudflare": "betterauth-d1-cloudflare",
    "cf": "betterauth-d1-cloudflare",
    "saas": "b2bsaas-cloudflare",
    "blog": "blog-cloudflare",
    "todos": "todos-cloudflare",
    "empty": "empty-cloudflare",
    "scaffold": "empty-cloudflare",
};
export function getTemplate(nameOrAlias) {
    const resolved = TEMPLATE_ALIASES[nameOrAlias] || nameOrAlias;
    return TEMPLATE_REGISTRY[resolved];
}
export function listTemplates() {
    return Object.values(TEMPLATE_REGISTRY);
}
export function listTemplateNames() {
    return Object.keys(TEMPLATE_REGISTRY);
}
export function listAliases() {
    return TEMPLATE_ALIASES;
}
export function formatTemplateList() {
    const lines = ["Available templates:\n"];
    for (const [id, template] of Object.entries(TEMPLATE_REGISTRY)) {
        lines.push(`  ${id}`);
        lines.push(`    ${template.description}`);
        lines.push(`    Tags: ${template.tags.join(", ")}`);
        lines.push("");
    }
    lines.push("Aliases:");
    for (const [alias, target] of Object.entries(TEMPLATE_ALIASES)) {
        lines.push(`  ${alias} -> ${target}`);
    }
    return lines.join("\n");
}
//# sourceMappingURL=registry.js.map