import {
  assertTenantId
} from "./chunk-6DWFJNIK.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 {
  DRIZZLE
} from "./chunk-U64T4YZE.js";
import {
  __decorateClass,
  __decorateParam
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/bridge/event-flow.service.ts
import { Inject, Injectable, Optional } from "@nestjs/common";
var EventFlowService = class {
  constructor(db, eventBus, orchestrator, bridgeRepo, registry = {}, multiTenant = false) {
    this.db = db;
    this.eventBus = eventBus;
    this.orchestrator = orchestrator;
    this.bridgeRepo = bridgeRepo;
    this.registry = registry;
    this.multiTenant = multiTenant;
  }
  db;
  eventBus;
  orchestrator;
  bridgeRepo;
  registry;
  multiTenant;
  async publish(event, tx) {
    await this.eventBus.publish(event, tx);
  }
  async publishAndStart(event, jobType, input, opts = {}) {
    assertTenantId(
      "EventFlowService.publishAndStart",
      this.multiTenant,
      opts.tenantId
    );
    const tenantId = opts.tenantId ?? null;
    const matchingTriggers = this.matchingTriggers(event.type, jobType);
    return this.db.transaction(async (tx) => {
      await this.eventBus.publish(event, tx);
      const run = await this.orchestrator.start(
        jobType,
        input,
        {
          parentRunId: opts.parentRunId,
          tenantId,
          triggerSource: "event",
          triggerRef: event.id
        },
        tx
      );
      const now = /* @__PURE__ */ new Date();
      for (const trigger of matchingTriggers) {
        await this.bridgeRepo.insertDelivery(
          {
            eventId: event.id,
            triggerId: trigger.triggerId,
            wrapperRunId: null,
            // facade never writes a wrapper
            userRunId: run.id,
            status: "delivered",
            tenantId,
            attemptedAt: now,
            deliveredAt: now
          },
          tx
        );
      }
      return { runId: run.id };
    });
  }
  /**
   * Linear scan of the per-event-type trigger list for entries whose
   * `jobType` matches. Typical N is 1–5; the table is not big enough to
   * warrant a secondary index. Returns an empty array for Case A.
   */
  matchingTriggers(eventType, jobType) {
    const triggers = this.registry[eventType] ?? [];
    return triggers.filter((t) => t.jobType === jobType);
  }
};
EventFlowService = __decorateClass([
  Injectable(),
  __decorateParam(0, Inject(DRIZZLE)),
  __decorateParam(1, Inject(EVENT_BUS)),
  __decorateParam(2, Inject(JOB_ORCHESTRATOR)),
  __decorateParam(3, Inject(BRIDGE_DELIVERY_REPO)),
  __decorateParam(4, Optional()),
  __decorateParam(4, Inject(BRIDGE_REGISTRY)),
  __decorateParam(5, Optional()),
  __decorateParam(5, Inject(BRIDGE_MULTI_TENANT))
], EventFlowService);

export {
  EventFlowService
};
//# sourceMappingURL=chunk-5RT7JGKT.js.map