import { defineHandler } from '@kb-labs/sdk';
import { loadBaseline, loadLastRun, compareWithBaseline } from '@kb-labs/qa-core';

// src/rest/handlers/baseline-diff-handler.ts
var baseline_diff_handler_default = defineHandler({
  async execute(ctx, _input) {
    const rootDir = ctx.cwd;
    const baseline = loadBaseline(rootDir);
    const lastRun = loadLastRun(rootDir);
    if (!baseline || !lastRun) {
      return {
        hasDiff: false,
        diff: {},
        baseline: baseline ?? null,
        current: lastRun ? {
          timestamp: lastRun.timestamp,
          summary: buildSummaryFromResults(lastRun.results)
        } : null
      };
    }
    const diff = compareWithBaseline(lastRun.results, baseline);
    const hasDiff = Object.keys(diff).some((ct) => {
      const d = diff[ct];
      return d.newFailures.length > 0 || d.fixed.length > 0;
    });
    return {
      hasDiff,
      diff,
      baseline,
      current: {
        timestamp: lastRun.timestamp,
        summary: buildSummaryFromResults(lastRun.results)
      }
    };
  }
});
function buildSummaryFromResults(results) {
  const summary = {};
  for (const ct of Object.keys(results)) {
    const r = results[ct];
    summary[ct] = {
      passed: r?.passed.length ?? 0,
      failed: r?.failed.length ?? 0
    };
  }
  return summary;
}

export { baseline_diff_handler_default as default };
//# sourceMappingURL=baseline-diff-handler.js.map
//# sourceMappingURL=baseline-diff-handler.js.map