import { defineCommand } from '@kb-labs/sdk';
import { analyzeTypes } from '@kb-labs/quality-core/types';
import { CACHE_KEYS } from '@kb-labs/quality-contracts';

// src/cli/commands/check-types.ts
var check_types_default = defineCommand({
  id: "quality:check-types",
  description: "Analyze TypeScript type safety across monorepo",
  handler: {
    async execute(ctx, input) {
      const { ui, platform } = ctx;
      const flags = input.flags ?? input;
      const cacheKey = `${CACHE_KEYS.TYPE_ANALYSIS}:${flags.package || "all"}`;
      if (!flags.refresh) {
        const cached = await platform.cache.get(cacheKey);
        if (cached) {
          outputTypeAnalysis({ ...cached, cached: true }, flags, ui);
          return { exitCode: cached.totalErrors > 0 ? 1 : 0, result: cached };
        }
      }
      const result = await analyzeTypes(ctx.cwd, {
        packageFilter: flags.package,
        errorsOnly: flags["errors-only"]
      });
      await platform.cache.set(cacheKey, result, 10 * 60 * 1e3);
      await platform.analytics.track("quality:check-types", {
        packageFilter: flags.package || "all",
        totalPackages: result.totalPackages,
        totalErrors: result.totalErrors,
        avgCoverage: result.avgCoverage,
        cached: false
      });
      outputTypeAnalysis({ ...result, cached: false }, flags, ui);
      return {
        exitCode: result.totalErrors > 0 ? 1 : 0,
        result,
        meta: { cached: false }
      };
    }
  }
});
function outputTypeAnalysis(result, flags, ui) {
  if (flags.json) {
    ui?.json?.(result);
    return;
  }
  const errorsOnly = flags["errors-only"] || false;
  const sections = [];
  const statusIcon = result.totalErrors === 0 ? "\u2705" : "\u274C";
  const statusText = result.totalErrors === 0 ? "All packages passed type checks" : "Type errors detected";
  const statusItems = [
    `${statusIcon} ${statusText}`,
    `Analyzed: ${result.totalPackages} package(s)`,
    `Errors: ${result.totalErrors}`,
    `Warnings: ${result.totalWarnings}`,
    `Avg Coverage: ${result.avgCoverage.toFixed(1)}%`
  ];
  sections.push({ header: "Type Safety Status", items: statusItems });
  if (result.packagesWithErrors > 0) {
    const errorItems = [];
    const packagesToShow = result.packages.filter((pkg) => !errorsOnly || pkg.errors > 0).slice(0, 10);
    for (const pkg of packagesToShow) {
      const status = pkg.errors > 0 ? "\u274C" : "\u2705";
      errorItems.push(`${status} ${pkg.name}`);
      const details = [
        pkg.errors > 0 ? `${pkg.errors} error(s)` : null,
        pkg.warnings > 0 ? `${pkg.warnings} warning(s)` : null,
        `${pkg.coverage.toFixed(1)}% coverage`,
        pkg.anyCount > 0 ? `${pkg.anyCount} any` : null,
        pkg.tsIgnoreCount > 0 ? `${pkg.tsIgnoreCount} @ts-ignore` : null
      ].filter(Boolean).join(", ");
      errorItems.push(`  ${details}`);
    }
    if (result.packages.length > 10) {
      errorItems.push(`... and ${result.packages.length - 10} more packages`);
    }
    sections.push({
      header: `Packages with Type Errors (${result.packagesWithErrors})`,
      items: errorItems
    });
  }
  const excellent = result.packages.filter((p) => p.coverage >= 90).length;
  const good = result.packages.filter((p) => p.coverage >= 70 && p.coverage < 90).length;
  const poor = result.packages.filter((p) => p.coverage < 70).length;
  if (result.packages.length > 0) {
    const coverageItems = [
      `\u2705 Excellent (\u226590%): ${excellent} package(s)`,
      `\u26A0\uFE0F  Good (70-90%):   ${good} package(s)`,
      poor > 0 ? `\u274C Poor (<70%):     ${poor} package(s)` : null
    ].filter(Boolean);
    sections.push({ header: "Type Coverage Distribution", items: coverageItems });
  }
  const summaryItems = [
    `Total packages: ${result.totalPackages}`,
    `Duration: ${(result.duration / 1e3).toFixed(1)}s`,
    result.cached ? "\u{1F4BE} Cached (use --refresh to recheck)" : "\u{1F504} Fresh analysis"
  ];
  sections.push({ header: "Summary", items: summaryItems });
  const title = result.totalErrors === 0 ? "\u2705 All Type Checks Passed" : `\u274C ${result.packagesWithErrors} Package(s) with Type Errors`;
  ui?.success?.("Type analysis completed", {
    title,
    sections
  });
}

export { check_types_default as default };
//# sourceMappingURL=check-types.js.map
//# sourceMappingURL=check-types.js.map