import { defineHandler } from '@kb-labs/sdk';
import { getWorkspacePackages, runTestCheck, runTypeCheck, runLintCheck } from '@kb-labs/qa-core';

// src/rest/handlers/run-check-handler.ts
var run_check_handler_default = defineHandler({
  async execute(ctx, input) {
    const rootDir = ctx.cwd;
    const startTime = Date.now();
    const body = input.body;
    const { checkType, repo, package: packageFilter } = body;
    let packages = getWorkspacePackages(rootDir);
    if (repo) {
      packages = packages.filter((p) => p.repo === repo);
    }
    if (packageFilter) {
      packages = packages.filter((p) => p.name === packageFilter || p.name.includes(packageFilter));
    }
    let result;
    switch (checkType) {
      case "lint":
        result = runLintCheck({ rootDir, packages });
        break;
      case "typeCheck":
        result = runTypeCheck({ rootDir, packages });
        break;
      case "test":
        result = runTestCheck({ rootDir, packages });
        break;
      default:
        result = { passed: [], failed: [], skipped: [], errors: {} };
    }
    const hasFailed = result.failed.length > 0;
    const durationMs = Date.now() - startTime;
    const analytics = ctx.platform.analytics;
    if (analytics) {
      await analytics.track("qa.check.completed", {
        checkType,
        status: hasFailed ? "failed" : "passed",
        passed: result.passed.length,
        failed: result.failed.length,
        skipped: result.skipped.length,
        durationMs,
        repo: repo || void 0,
        package: packageFilter || void 0
      });
    }
    return {
      checkType,
      status: hasFailed ? "failed" : "passed",
      result,
      durationMs
    };
  }
});

export { run_check_handler_default as default };
//# sourceMappingURL=run-check-handler.js.map
//# sourceMappingURL=run-check-handler.js.map