import { defineHandler } from '@kb-labs/sdk';
import { ConventionalTypeSchema, COMMIT_CACHE_PREFIX } from '@kb-labs/commit-contracts';
import { loadPlan, savePlan } from '@kb-labs/commit-core/storage';

// src/rest/handlers/patch-plan-handler.ts
var patch_plan_handler_default = defineHandler({
  async execute(ctx, input) {
    const { scope = "root", commitId, message, type, scope_: commitScope, body } = input.body ?? {};
    if (!commitId) {
      throw new Error("commitId is required");
    }
    const plan = await loadPlan(ctx.cwd, scope);
    if (!plan) {
      throw new Error("No commit plan found. Generate a plan first.");
    }
    const commitIndex = plan.commits.findIndex((c) => c.id === commitId);
    if (commitIndex === -1) {
      throw new Error(`Commit "${commitId}" not found in plan`);
    }
    const commit = plan.commits[commitIndex];
    if (message !== void 0) {
      commit.message = message;
    }
    if (type !== void 0) {
      const parsed = ConventionalTypeSchema.safeParse(type);
      if (parsed.success) {
        commit.type = parsed.data;
      }
    }
    if (commitScope !== void 0) {
      commit.scope = commitScope || void 0;
    }
    if (body !== void 0) {
      commit.body = body || void 0;
    }
    plan.commits[commitIndex] = commit;
    await savePlan(ctx.cwd, plan, scope);
    const appliedCacheKey = `${COMMIT_CACHE_PREFIX}plan-applied:${scope}`;
    await ctx.platform.cache.delete(appliedCacheKey);
    return {
      success: true,
      scope,
      commitId
    };
  }
});

export { patch_plan_handler_default as default };
//# sourceMappingURL=patch-plan-handler.js.map
//# sourceMappingURL=patch-plan-handler.js.map