import {
  DrizzleCacheService
} from "./chunk-T6C4LFLC.js";
import {
  MemoryCacheService
} from "./chunk-IF5I3DAA.js";
import {
  CACHE,
  CACHE_DEFAULT_TTL
} from "./chunk-L6FTY45T.js";
import {
  DRIZZLE
} from "./chunk-U64T4YZE.js";
import {
  __decorateClass
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/cache/cache.module.ts
import { Module } from "@nestjs/common";
var CACHE_MODULE_OPTIONS = "CACHE_MODULE_OPTIONS";
function buildCacheAsync(options, db) {
  const defaultTtl = options.defaultTtl ?? null;
  if (options.backend === "drizzle") {
    if (!db) {
      throw new Error(
        "CacheModule.forRootAsync: backend: 'drizzle' selected but DRIZZLE provider is not available. Ensure DatabaseModule (or another provider exposing DRIZZLE) is imported before CacheModule.forRootAsync."
      );
    }
    return new DrizzleCacheService(db, defaultTtl);
  }
  return new MemoryCacheService(defaultTtl);
}
var CacheModule = class {
  static forRootAsync(asyncOptions) {
    return {
      module: CacheModule,
      global: true,
      imports: asyncOptions.imports ?? [],
      providers: [
        {
          provide: CACHE_MODULE_OPTIONS,
          useFactory: asyncOptions.useFactory,
          inject: asyncOptions.inject ?? []
        },
        {
          provide: CACHE,
          useFactory: (options, db) => buildCacheAsync(options, db),
          inject: [CACHE_MODULE_OPTIONS, { token: DRIZZLE, optional: true }]
        },
        // Alias the concrete classes to CACHE for typed injection.
        { provide: DrizzleCacheService, useExisting: CACHE },
        { provide: MemoryCacheService, useExisting: CACHE }
      ],
      exports: [CACHE]
    };
  }
  static forRoot(options = { backend: "drizzle" }) {
    const ConcreteClass = options.backend === "drizzle" ? DrizzleCacheService : MemoryCacheService;
    const providers = options.defaultTtl !== void 0 ? [
      // Register the concrete class as the canonical instance
      ConcreteClass,
      { provide: CACHE_DEFAULT_TTL, useValue: options.defaultTtl },
      // CACHE token points to the same instance — no duplicate
      { provide: CACHE, useExisting: ConcreteClass }
    ] : [
      ConcreteClass,
      { provide: CACHE, useExisting: ConcreteClass }
    ];
    return {
      module: CacheModule,
      global: true,
      providers,
      exports: [CACHE]
    };
  }
};
CacheModule = __decorateClass([
  Module({})
], CacheModule);

export {
  CacheModule
};
//# sourceMappingURL=chunk-COGHTKXY.js.map