/**
 * analyze_hono_app — complete Hono application overview.
 *
 * Returns routes grouped by method/scope, middleware map, context variables,
 * OpenAPI status, RPC exports (with slow-pattern detection), runtime,
 * and env bindings in one call.
 *
 * Spec: docs/specs/2026-04-10-hono-framework-intelligence-spec.md (Task 17)
 */
import { getCodeIndex } from "./index-tools.js";
import { honoCache } from "../cache/hono-cache.js";
import { HonoExtractor } from "../parser/extractors/hono.js";
import { resolveHonoEntryFile } from "./hono-entry-resolver.js";
import { detectFrameworks } from "../utils/framework-detect.js";
export async function analyzeHonoApp(repo, entryFile, forceRefresh) {
    const index = await getCodeIndex(repo);
    if (!index) {
        return { error: `Repository "${repo}" not found` };
    }
    const frameworks = detectFrameworks(index);
    if (!frameworks.has("hono")) {
        return { error: "No Hono app detected in this repo" };
    }
    const resolvedEntry = entryFile ?? resolveHonoEntryFile(index);
    if (!resolvedEntry) {
        return { error: "No Hono app entry file found" };
    }
    if (forceRefresh) {
        honoCache.clear(repo);
    }
    let model;
    try {
        model = await honoCache.get(repo, resolvedEntry, new HonoExtractor());
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        return { error: `Failed to parse Hono app: ${msg}` };
    }
    // Routes grouped by method and mount prefix
    const byMethod = {};
    const byMount = {};
    for (const route of model.routes) {
        byMethod[route.method] = (byMethod[route.method] ?? 0) + 1;
        const segments = route.path.split("/").filter(Boolean);
        const mountKey = segments.length >= 2 ? `/${segments[0]}/${segments[1]}` : `/${segments[0] ?? ""}`;
        byMount[mountKey] = (byMount[mountKey] ?? 0) + 1;
    }
    // Middleware grouped by scope, third-party list
    const byScope = {};
    const thirdParty = new Set();
    for (const mc of model.middleware_chains) {
        byScope[mc.scope] = (byScope[mc.scope] ?? 0) + mc.entries.length;
        for (const entry of mc.entries) {
            if (entry.is_third_party)
                thirdParty.add(entry.name);
        }
    }
    return {
        framework: "hono",
        runtime: model.runtime,
        entry_file: model.entry_file,
        routes: {
            total: model.routes.length,
            by_method: byMethod,
            by_mount: byMount,
        },
        middleware: {
            total_chains: model.middleware_chains.length,
            by_scope: byScope,
            third_party: [...thirdParty].sort(),
        },
        context_vars: model.context_vars.map((cv) => ({
            name: cv.name,
            set_count: cv.set_points.length,
            get_count: cv.get_points.length,
            is_env_binding: cv.is_env_binding,
        })),
        openapi: {
            enabled: model.openapi_routes.length > 0,
            route_count: model.openapi_routes.length,
        },
        rpc_exports: model.rpc_exports.map((r) => ({
            name: r.export_name,
            shape: r.shape,
            is_slow_pattern: r.shape === "full_app",
            file: r.file,
        })),
        env_bindings: model.env_bindings,
        extraction_status: model.extraction_status,
        skip_reasons: model.skip_reasons,
        files_used_count: model.files_used.length,
    };
}
//# sourceMappingURL=hono-analyze-app.js.map