import {
  assertTenantId
} from "./chunk-6DWFJNIK.js";
import {
  JobHandler,
  JobHandlerBase
} from "./chunk-CO6LUM72.js";
import {
  JOB_ORCHESTRATOR
} from "./chunk-ZPL74UQN.js";
import {
  BRIDGE_DELIVERY_REPO,
  BRIDGE_MULTI_TENANT,
  BRIDGE_REGISTRY
} from "./chunk-4LH67P4U.js";
import {
  EVENT_BUS
} from "./chunk-H5NH7KPE.js";
import {
  __decorateClass,
  __decorateParam
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/bridge/bridge-delivery-handler.ts
import { Inject, Injectable, Logger, Optional } from "@nestjs/common";
var BRIDGE_DELIVERY_JOB_TYPE = "@framework/bridge_delivery";
var SPAWN_USER_RUN_STEP = "spawn_user_run";
var BridgeDeliveryHandler = class extends JobHandlerBase {
  constructor(repo, orchestrator, events, registry, multiTenant = false) {
    super();
    this.repo = repo;
    this.orchestrator = orchestrator;
    this.events = events;
    this.registry = registry;
    this.multiTenant = multiTenant;
  }
  repo;
  orchestrator;
  events;
  registry;
  multiTenant;
  classLogger = new Logger(BridgeDeliveryHandler.name);
  async run(ctx) {
    const { deliveryId } = ctx.input;
    const delivery = await this.repo.findDeliveryById(deliveryId);
    if (!delivery) {
      this.classLogger.warn(
        `bridge_delivery row '${deliveryId}' not found; wrapper completes without spawning a user job.`
      );
      return { skipped: true, reason: "delivery_row_missing" };
    }
    assertTenantId(
      "BridgeDeliveryHandler.run",
      this.multiTenant,
      delivery.tenantId
    );
    const event = await this.events.findById(delivery.eventId);
    if (!event) {
      this.classLogger.warn(
        `domain_events row '${delivery.eventId}' missing for delivery '${deliveryId}'; marking skipped.`
      );
      await this.repo.markSkipped(delivery.id, "event_row_missing");
      return { skipped: true, reason: "event_row_missing" };
    }
    const entry = this.findRegistryEntry(event.type, delivery.triggerId);
    if (!entry) {
      await this.repo.markSkipped(delivery.id, "trigger_unregistered");
      return { skipped: true, reason: "trigger_unregistered" };
    }
    if (entry.when && !entry.when(event)) {
      await this.repo.markSkipped(delivery.id, "predicate_false");
      return { skipped: true, reason: "predicate_false" };
    }
    const input = entry.map(event);
    const { runId } = await ctx.step(
      SPAWN_USER_RUN_STEP,
      async () => {
        const run = await this.orchestrator.start(entry.jobType, input, {
          parentRunId: ctx.run.id,
          triggerSource: "event",
          triggerRef: delivery.eventId,
          tenantId: delivery.tenantId
        });
        return { runId: run.id };
      }
    );
    await this.repo.markDelivered(delivery.id, runId);
    return { runId };
  }
  /**
   * Locate the registry entry for `(eventType, triggerId)`. Linear scan
   * over the per-event-type array — N is the number of triggers declared
   * for one event, typically 1–5; the table is not big enough to warrant
   * a secondary index.
   */
  findRegistryEntry(eventType, triggerId) {
    const candidates = this.registry[eventType] ?? void 0;
    if (!candidates) return void 0;
    return candidates.find((c) => c.triggerId === triggerId);
  }
};
BridgeDeliveryHandler = __decorateClass([
  Injectable(),
  JobHandler(BRIDGE_DELIVERY_JOB_TYPE, {
    pool: "events_change",
    retry: { attempts: 3, backoff: "exponential", baseMs: 250 },
    replayFrom: "last_step"
  }),
  __decorateParam(0, Inject(BRIDGE_DELIVERY_REPO)),
  __decorateParam(1, Inject(JOB_ORCHESTRATOR)),
  __decorateParam(2, Inject(EVENT_BUS)),
  __decorateParam(3, Inject(BRIDGE_REGISTRY)),
  __decorateParam(4, Optional()),
  __decorateParam(4, Inject(BRIDGE_MULTI_TENANT))
], BridgeDeliveryHandler);

export {
  BRIDGE_DELIVERY_JOB_TYPE,
  BridgeDeliveryHandler
};
//# sourceMappingURL=chunk-NXNVTXKG.js.map