import { defineHandler, useConfig } from '@kb-labs/sdk';
import { getGitStatus } from '@kb-labs/commit-core/analyzer';
import { resolveCommitConfig } from '@kb-labs/commit-contracts';
import * as path from 'path';

// src/rest/handlers/git-status-handler.ts
function resolveScopePath(baseCwd, scopeId = "root", scopes) {
  const scopeDef = scopes?.find((s) => s.id === scopeId);
  const relativePath = scopeDef?.path ?? (scopeId === "root" ? "." : scopeId);
  return relativePath === "." ? baseCwd : path.join(baseCwd, relativePath);
}

// src/rest/handlers/git-status-handler.ts
var git_status_handler_default = defineHandler({
  async execute(ctx, input) {
    const scope = input.query?.scope || "root";
    try {
      const fileConfig = await useConfig();
      const config = resolveCommitConfig(fileConfig ?? {});
      const scopeCwd = resolveScopePath(ctx.cwd, scope, config.scope?.scopes);
      ctx.platform.logger.info("[git-status] Resolution", {
        cwd: ctx.cwd,
        scope,
        scopeCwd
      });
      const status = await getGitStatus(scopeCwd);
      const rows = [];
      for (const filePath of status.staged) {
        rows.push({
          path: filePath,
          status: "staged"
        });
      }
      for (const filePath of status.unstaged) {
        rows.push({
          path: filePath,
          status: "modified"
        });
      }
      for (const filePath of status.untracked) {
        rows.push({
          path: filePath,
          status: "untracked"
        });
      }
      return {
        rows,
        total: rows.length
      };
    } catch (error) {
      throw new Error(`Failed to get git status: ${error}`);
    }
  }
});

export { git_status_handler_default as default };
//# sourceMappingURL=git-status-handler.js.map
//# sourceMappingURL=git-status-handler.js.map