import {
  bridgeRegistry
} from "./chunk-5A432NZJ.js";
import {
  EventFlowService
} from "./chunk-5RT7JGKT.js";
import {
  BRIDGE_RESERVED_POOLS
} from "./chunk-EDKJU5BO.js";
import {
  DrizzleBridgeDeliveryRepo
} from "./chunk-KSTZIULO.js";
import {
  MemoryBridgeDeliveryRepo
} from "./chunk-4DOJBQTP.js";
import {
  BridgeOutboxDrainHook
} from "./chunk-SFQRETXJ.js";
import {
  BridgeDeliveryHandler
} from "./chunk-SGSWVNNB.js";
import {
  BridgeReservedPoolsNotPolledError
} from "./chunk-NXXDZ6ZF.js";
import {
  JOB_WORKER_MODULE_OPTIONS
} from "./chunk-HPS554L4.js";
import {
  BRIDGE_DELIVERY_REPO,
  BRIDGE_MODULE_OPTIONS,
  BRIDGE_MULTI_TENANT,
  BRIDGE_OUTBOX_DRAIN_HOOK,
  BRIDGE_REGISTRY,
  EVENT_FLOW
} from "./chunk-4LH67P4U.js";
import {
  __decorateClass,
  __decorateParam
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/bridge/bridge.module.ts
import {
  Inject,
  Module,
  Optional
} from "@nestjs/common";
var BridgeModule = class {
  /**
   * `JOB_WORKER_MODULE_OPTIONS` is declared `@Optional()` so unit tests
   * that mount `BridgeModule` alone (no `JobWorkerModule`) boot
   * cleanly — the boot-time check skips when the token is undefined.
   */
  constructor(workerOpts) {
    this.workerOpts = workerOpts;
  }
  workerOpts;
  static forRoot(opts) {
    const repoProvider = opts.backend === "memory" ? { provide: BRIDGE_DELIVERY_REPO, useClass: MemoryBridgeDeliveryRepo } : { provide: BRIDGE_DELIVERY_REPO, useClass: DrizzleBridgeDeliveryRepo };
    return {
      module: BridgeModule,
      global: true,
      // BridgeModule consumes EVENT_BUS / JOB_ORCHESTRATOR / DRIZZLE
      // from sibling subsystems via DI; no `imports` needed here. The
      // consumer is responsible for wiring EventsModule + JobsDomainModule
      // (or JobWorkerModule, which transitively imports the latter)
      // BEFORE BridgeModule.
      providers: [
        { provide: BRIDGE_MODULE_OPTIONS, useValue: opts },
        { provide: BRIDGE_MULTI_TENANT, useValue: opts.multiTenant ?? false },
        // Package mode threads the consumer's generated registry through
        // `opts.registry`; vendored mode omits it and we fall back to the
        // bundled `./generated/registry` (which IS the consumer's generated
        // file in a vendored tree). See `BridgeModuleOptions.registry`.
        { provide: BRIDGE_REGISTRY, useValue: opts.registry ?? bridgeRegistry },
        repoProvider,
        // Drain hook — always wired; `DrizzleEventBus` consumes it via
        // `@Optional()`, so non-bridge mounts simply see `undefined`.
        { provide: BRIDGE_OUTBOX_DRAIN_HOOK, useClass: BridgeOutboxDrainHook },
        // Facade — class provider + token alias.
        EventFlowService,
        { provide: EVENT_FLOW, useExisting: EventFlowService },
        // Framework handler — provider so DI can construct it. The
        // `@JobHandler` decorator already auto-registers it in
        // `JOB_HANDLER_REGISTRY` at module-load time, and its `jobs`
        // row is upserted at `JobWorkerModule.onModuleInit`. We just
        // need the class instantiated as a Nest provider so its DI
        // deps (BRIDGE_DELIVERY_REPO, JOB_ORCHESTRATOR, EVENT_BUS,
        // BRIDGE_REGISTRY, BRIDGE_MULTI_TENANT) resolve.
        BridgeDeliveryHandler
      ],
      exports: [
        EVENT_FLOW,
        BRIDGE_DELIVERY_REPO,
        BRIDGE_REGISTRY,
        BRIDGE_MULTI_TENANT,
        BRIDGE_MODULE_OPTIONS,
        BRIDGE_OUTBOX_DRAIN_HOOK
      ]
    };
  }
  async onModuleInit() {
    if (!this.workerOpts) return;
    if (this.workerOpts.allPools) return;
    const activePools = this.workerOpts.pools ?? [];
    const missing = BRIDGE_RESERVED_POOLS.filter(
      (p) => !activePools.includes(p)
    );
    if (missing.length > 0) {
      throw new BridgeReservedPoolsNotPolledError(missing);
    }
  }
};
BridgeModule = __decorateClass([
  Module({}),
  __decorateParam(0, Optional()),
  __decorateParam(0, Inject(JOB_WORKER_MODULE_OPTIONS))
], BridgeModule);

export {
  BridgeModule
};
//# sourceMappingURL=chunk-PSDVGPQR.js.map