import { LocalStorageDriver } from "./localDriver.js";
import { LegacyPublicStorageDriver } from "./legacyPublicDriver.js";
import { AttachmentPartition } from "../../data/entities.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("attachments").child({ component: "storage-driver-factory" });
const moduleDriverRegistry = /* @__PURE__ */ new Map();
const moduleEnhancerRegistry = /* @__PURE__ */ new Map();
function registerExternalStorageDriver(key, factory) {
  moduleDriverRegistry.set(key, { factory });
}
function registerExternalCredentialEnhancer(key, enhancer) {
  moduleEnhancerRegistry.set(key, { enhancer });
}
class StorageDriverFactory {
  constructor(em) {
    this.em = em;
    this.cache = /* @__PURE__ */ new Map();
    this.localDriver = new LocalStorageDriver();
    this.legacyPublicDriver = new LegacyPublicStorageDriver();
    this.externalDrivers = /* @__PURE__ */ new Map();
    this.credentialEnhancers = /* @__PURE__ */ new Map();
  }
  registerDriver(key, factory) {
    this.externalDrivers.set(key, factory);
  }
  registerCredentialEnhancer(key, enhancer) {
    this.credentialEnhancers.set(key, enhancer);
  }
  resolveForAttachment(storageDriver, configJson) {
    switch (storageDriver) {
      case "legacyPublic":
        return this.legacyPublicDriver;
      case "local":
        return this.localDriver;
      default: {
        const externalFactory = this.externalDrivers.get(storageDriver) ?? moduleDriverRegistry.get(storageDriver)?.factory;
        if (externalFactory) {
          const cacheKey = `${storageDriver}:${JSON.stringify(configJson ?? {})}`;
          const cached = this.cache.get(cacheKey);
          if (cached) return cached;
          const driver = externalFactory(configJson ?? {});
          this.cache.set(cacheKey, driver);
          return driver;
        }
        return this.localDriver;
      }
    }
  }
  async resolveForPartition(partitionCode, scope) {
    const partition = await this.em.findOne(AttachmentPartition, { code: partitionCode });
    if (!partition) return this.localDriver;
    const driverKey = partition.storageDriver ?? "local";
    logger.debug("Resolving storage driver for partition", { partition: partitionCode, driverKey, moduleDrivers: [...moduleDriverRegistry.keys()], instanceDrivers: [...this.externalDrivers.keys()] });
    let config = partition.configJson ?? {};
    const activeEnhancer = this.credentialEnhancers.get(driverKey) ?? moduleEnhancerRegistry.get(driverKey)?.enhancer;
    if (scope && activeEnhancer) {
      config = await activeEnhancer(config, scope);
      const externalFactory = this.externalDrivers.get(driverKey) ?? moduleDriverRegistry.get(driverKey)?.factory;
      if (externalFactory) return externalFactory(config);
    }
    return this.resolveForAttachment(driverKey, config);
  }
}
export {
  StorageDriverFactory,
  registerExternalCredentialEnhancer,
  registerExternalStorageDriver
};
//# sourceMappingURL=driverFactory.js.map
