import {
  MemoryJobStore
} from "./chunk-SNQ3TOWP.js";
import {
  __decorateClass,
  __decorateParam
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/jobs/job-step-service.memory-backend.ts
import { randomUUID } from "crypto";
import { Inject, Injectable } from "@nestjs/common";
var MemoryJobStepService = class {
  // ADR-037 (package-mode DI): explicit `@Inject(MemoryJobStore)` — the
  // published bundle carries no `design:paramtypes`, so a by-type inject
  // would resolve to `undefined` in package mode.
  constructor(store) {
    this.store = store;
  }
  store;
  async findStep(runId, stepId) {
    const rows = this.store.steps.get(runId);
    if (!rows) return null;
    const match = rows.find(
      (r) => r.stepId === stepId && r.status === "completed"
    );
    return match ?? null;
  }
  async recordStep(input) {
    const rows = this.getOrCreateRows(input.jobRunId);
    const existingIdx = rows.findIndex((r) => r.stepId === input.stepId);
    const normalisedInput = input.input ?? null;
    const normalisedOutput = input.output ?? null;
    if (existingIdx >= 0) {
      const prev = rows[existingIdx];
      const next = {
        ...prev,
        status: input.status,
        input: normalisedInput ?? prev.input,
        output: normalisedOutput ?? prev.output,
        error: input.error ?? prev.error,
        attempts: input.attempts ?? prev.attempts,
        startedAt: input.startedAt ?? prev.startedAt,
        finishedAt: input.finishedAt ?? prev.finishedAt
      };
      rows[existingIdx] = next;
      return next;
    }
    const seq = input.seq ?? this.nextSeq(rows);
    const row = {
      id: randomUUID(),
      jobRunId: input.jobRunId,
      stepId: input.stepId,
      kind: input.kind,
      seq,
      status: input.status,
      input: normalisedInput,
      output: normalisedOutput,
      error: input.error ?? null,
      attempts: input.attempts ?? 0,
      startedAt: input.startedAt ?? null,
      finishedAt: input.finishedAt ?? null
    };
    rows.push(row);
    return row;
  }
  /**
   * Replay helper — wipe every step row for a run. Mirrors the `scratch`
   * replay mode of the Drizzle backend (`DELETE FROM job_step WHERE job_run_id = …`).
   */
  clearStepsForRun(runId) {
    this.store.steps.delete(runId);
  }
  /**
   * Remove every non-`completed` row for the run. Memoized (`completed`)
   * rows are preserved — this is the `last_checkpoint` / `last_step`
   * semantics the Drizzle backend implements via
   * `DELETE … WHERE status != 'completed'`. Both replay modes route here
   * (Phase 1 collapses `last_step` onto this behaviour; see JOB-3 notes).
   */
  clearIncompleteSteps(runId) {
    const rows = this.store.steps.get(runId);
    if (!rows) return;
    const kept = rows.filter((r) => r.status === "completed");
    if (kept.length === 0) {
      this.store.steps.delete(runId);
    } else {
      this.store.steps.set(runId, kept);
    }
  }
  getOrCreateRows(runId) {
    let rows = this.store.steps.get(runId);
    if (!rows) {
      rows = [];
      this.store.steps.set(runId, rows);
    }
    return rows;
  }
  nextSeq(rows) {
    let max = 0;
    for (const r of rows) {
      if (r.seq > max) max = r.seq;
    }
    return max + 1;
  }
};
MemoryJobStepService = __decorateClass([
  Injectable(),
  __decorateParam(0, Inject(MemoryJobStore))
], MemoryJobStepService);

export {
  MemoryJobStepService
};
//# sourceMappingURL=chunk-PNZSGAB2.js.map