import {
  TypedEventBus
} from "./chunk-GM3RMJIJ.js";
import {
  DrizzleEventBus
} from "./chunk-H6FO2ZDJ.js";
import {
  MemoryEventBus
} from "./chunk-LQ6PYFU6.js";
import {
  EVENTS_MODULE_OPTIONS,
  EVENTS_MULTI_TENANT,
  EVENT_BUS,
  EVENT_READ_PORT,
  REDIS_URL,
  TYPED_EVENT_BUS
} from "./chunk-H5NH7KPE.js";
import {
  DRIZZLE
} from "./chunk-U64T4YZE.js";
import {
  __decorateClass
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/events/events.module.ts
import { Module } from "@nestjs/common";
async function loadRedisEventBus() {
  const specifier = "./event-bus.redis-backend";
  const mod = await import(specifier);
  return mod.RedisEventBus;
}
function buildTypedBusProviders(multiTenant, typedBus) {
  const BusClass = typedBus ?? TypedEventBus;
  return [
    BusClass,
    { provide: TYPED_EVENT_BUS, useExisting: BusClass },
    { provide: EVENTS_MULTI_TENANT, useValue: multiTenant }
  ];
}
async function buildEventBusAsync(options, db, redisUrl) {
  if (options.backend === "drizzle") {
    if (!db) {
      throw new Error(
        "EventsModule.forRootAsync: backend: 'drizzle' selected but DRIZZLE provider is not available. Ensure DatabaseModule (or another provider exposing DRIZZLE) is imported before EventsModule.forRootAsync."
      );
    }
    return new DrizzleEventBus(db, options);
  }
  if (options.backend === "redis") {
    const RedisEventBus = await loadRedisEventBus();
    return new RedisEventBus(redisUrl);
  }
  return new MemoryEventBus(options);
}
var EventsModule = class {
  static forRootAsync(asyncOptions) {
    return {
      module: EventsModule,
      global: true,
      imports: asyncOptions.imports ?? [],
      providers: [
        {
          provide: EVENTS_MODULE_OPTIONS,
          useFactory: asyncOptions.useFactory,
          inject: asyncOptions.inject ?? []
        },
        {
          provide: EVENTS_MULTI_TENANT,
          useFactory: (options) => options.multiTenant ?? false,
          inject: [EVENTS_MODULE_OPTIONS]
        },
        {
          provide: REDIS_URL,
          useFactory: (options) => options.redisUrl ?? process.env["REDIS_URL"] ?? "redis://localhost:6379",
          inject: [EVENTS_MODULE_OPTIONS]
        },
        {
          provide: EVENT_BUS,
          useFactory: (options, db, redisUrl) => buildEventBusAsync(options, db, redisUrl),
          inject: [
            EVENTS_MODULE_OPTIONS,
            { token: DRIZZLE, optional: true },
            REDIS_URL
          ]
        },
        {
          // Read port (OBS-LIST-1). Drizzle + memory backends implement
          // IEventReadPort on the EVENT_BUS instance; the redis backend
          // retains no history, so EVENT_READ_PORT resolves to `null` and
          // optional consumers (the observability combiner) degrade to
          // empty results.
          provide: EVENT_READ_PORT,
          useFactory: (options, bus) => options.backend === "redis" ? null : bus,
          inject: [EVENTS_MODULE_OPTIONS, EVENT_BUS]
        },
        TypedEventBus,
        { provide: TYPED_EVENT_BUS, useExisting: TypedEventBus }
      ],
      exports: [EVENT_BUS, EVENT_READ_PORT, TYPED_EVENT_BUS, EVENTS_MULTI_TENANT]
    };
  }
  static forRoot(options = { backend: "drizzle" }) {
    const multiTenant = options.multiTenant ?? false;
    if (options.backend === "redis") {
      const resolvedUrl = options.redisUrl ?? process.env["REDIS_URL"] ?? "redis://localhost:6379";
      return {
        module: EventsModule,
        global: true,
        providers: [
          { provide: EVENTS_MODULE_OPTIONS, useValue: options },
          { provide: REDIS_URL, useValue: resolvedUrl },
          {
            // #6: useFactory + dynamic import so the consumer's tsc never
            // needs to resolve `event-bus.redis-backend.ts` for drizzle/
            // memory installs (the file is filtered out by
            // `backendFileFilter`). Nest awaits async factories + manages
            // lifecycle on the returned instance, so we drop the old bare
            // `RedisEventBus` provider entry.
            provide: EVENT_BUS,
            useFactory: async (url) => {
              const RedisEventBus = await loadRedisEventBus();
              return new RedisEventBus(url);
            },
            inject: [REDIS_URL]
          },
          ...buildTypedBusProviders(multiTenant, options.typedBus)
        ],
        exports: [EVENT_BUS, TYPED_EVENT_BUS, EVENTS_MULTI_TENANT]
      };
    }
    const provider = options.backend === "drizzle" ? { provide: EVENT_BUS, useClass: DrizzleEventBus } : { provide: EVENT_BUS, useClass: MemoryEventBus };
    return {
      module: EventsModule,
      global: true,
      providers: [
        { provide: EVENTS_MODULE_OPTIONS, useValue: options },
        provider,
        // Read port (OBS-LIST-1): drizzle + memory backends implement
        // IEventReadPort on the same instance as EVENT_BUS. The redis
        // backend retains no history and does not provide this token.
        { provide: EVENT_READ_PORT, useExisting: EVENT_BUS },
        ...buildTypedBusProviders(multiTenant, options.typedBus)
      ],
      exports: [EVENT_BUS, EVENT_READ_PORT, TYPED_EVENT_BUS, EVENTS_MULTI_TENANT]
    };
  }
};
EventsModule = __decorateClass([
  Module({})
], EventsModule);

export {
  EventsModule
};
//# sourceMappingURL=chunk-TKVTEUBD.js.map