/**
 * Docs command - display built-in documentation
 *
 * Usage:
 *   quickback docs              # Show available topics
 *   quickback docs <topic>      # Show documentation for a topic
 *   quickback docs --list       # List all topics
 */
import pc from "picocolors";
import { DOCS, TOPIC_LIST } from "../docs/content.js";
import { renderMarkdown } from "../lib/markdown-renderer.js";
export async function docs(args) {
    const topic = args[0];
    // Show topic list
    if (!topic || topic === "--list" || topic === "-l") {
        console.log("");
        console.log(pc.bold("Quickback Documentation"));
        console.log(pc.dim("Built-in reference available offline\n"));
        console.log(pc.bold("Security Pillars"));
        printTopic("firewall", "Data isolation layer");
        printTopic("access", "Role-based permissions");
        printTopic("guards", "Field protection");
        printTopic("masking", "PII redaction");
        console.log("");
        console.log(pc.bold("Core Concepts"));
        printTopic("concepts", "Architecture overview");
        printTopic("config", "Configuration reference");
        printTopic("features", "Schema definitions");
        console.log("");
        console.log(pc.bold("API & Business Logic"));
        printTopic("api", "CRUD endpoints reference");
        printTopic("actions", "Custom business logic");
        printTopic("views", "Read-only aggregations");
        console.log("");
        console.log(pc.bold("Getting Started"));
        printTopic("quickstart", "Quick start guide");
        printTopic("cli", "CLI reference");
        printTopic("examples", "Example applications");
        console.log("");
        console.log(pc.bold("Deployment"));
        printTopic("cloudflare", "Cloudflare Workers setup");
        printTopic("d1", "D1 database configuration");
        console.log("");
        console.log(pc.dim("Usage: quickback docs <topic>"));
        console.log(pc.dim("Full docs: https://quickback.dev/docs\n"));
        return;
    }
    // Show specific topic — try exact match first, then fuzzy suffix match
    const resolved = resolveTopic(topic);
    if (!resolved) {
        console.log(pc.red(`\nUnknown topic: ${topic}`));
        console.log(pc.dim(`\nAvailable topics: ${TOPIC_LIST.join(", ")}`));
        console.log(pc.dim(`Run 'quickback docs' to see all topics\n`));
        process.exit(1);
    }
    const doc = DOCS[resolved];
    console.log("");
    console.log(renderMarkdown(doc.content));
    console.log("");
    console.log(pc.dim(`─`.repeat(60)));
    console.log(pc.dim(`Full documentation: https://quickback.dev/docs/${topic}`));
    console.log("");
}
/**
 * Resolve a topic name — exact match, then fuzzy suffix match.
 * Returns the resolved full topic key, or null if not found.
 * If multiple matches, prints disambiguation help and exits.
 */
function resolveTopic(input) {
    // Exact match
    if (DOCS[input])
        return input;
    // Suffix match: find topics ending with /{input} or equal to {input}
    const matches = TOPIC_LIST.filter((t) => t === input || t.endsWith(`/${input}`));
    if (matches.length === 1)
        return matches[0];
    if (matches.length > 1) {
        console.log(pc.yellow(`\nAmbiguous topic "${input}". Did you mean:\n`));
        for (const m of matches) {
            console.log(`  ${pc.cyan(m)}`);
        }
        console.log(pc.dim(`\nUse the full path, e.g.: quickback docs ${matches[0]}\n`));
        process.exit(1);
    }
    return null;
}
function printTopic(topic, fallbackDesc) {
    const doc = DOCS[topic];
    const title = doc?.title || fallbackDesc;
    const available = doc ? pc.cyan(topic.padEnd(14)) : pc.dim(topic.padEnd(14));
    console.log(`  ${available} ${title}`);
}
//# sourceMappingURL=docs.js.map