import { DEFAULT_WEBHOOK_BODY_LIMIT_BYTES } from "../../lib/webhooks/body.js";
const ADAPTER_REGISTRY_KEY = "__openMercatoPaymentGatewayAdapters__";
const WEBHOOK_REGISTRY_KEY = "__openMercatoPaymentGatewayWebhookHandlers__";
const DESCRIPTOR_REGISTRY_KEY = "__openMercatoPaymentGatewayDescriptors__";
function getAdapterRegistry() {
  const globalState = globalThis;
  if (!globalState[ADAPTER_REGISTRY_KEY]) {
    globalState[ADAPTER_REGISTRY_KEY] = /* @__PURE__ */ new Map();
  }
  return globalState[ADAPTER_REGISTRY_KEY];
}
function getWebhookHandlerRegistry() {
  const globalState = globalThis;
  if (!globalState[WEBHOOK_REGISTRY_KEY]) {
    globalState[WEBHOOK_REGISTRY_KEY] = /* @__PURE__ */ new Map();
  }
  return globalState[WEBHOOK_REGISTRY_KEY];
}
function getDescriptorRegistry() {
  const globalState = globalThis;
  if (!globalState[DESCRIPTOR_REGISTRY_KEY]) {
    globalState[DESCRIPTOR_REGISTRY_KEY] = /* @__PURE__ */ new Map();
  }
  return globalState[DESCRIPTOR_REGISTRY_KEY];
}
function adapterKey(providerKey, version) {
  return version ? `${providerKey}:${version}` : providerKey;
}
function registerGatewayAdapter(adapter, options) {
  const adapterRegistry = getAdapterRegistry();
  const key = adapterKey(adapter.providerKey, options?.version);
  adapterRegistry.set(key, adapter);
  if (options?.version) {
    if (!adapterRegistry.has(adapter.providerKey)) {
      adapterRegistry.set(adapter.providerKey, adapter);
    }
  }
  return () => {
    adapterRegistry.delete(key);
  };
}
function getGatewayAdapter(providerKey, version) {
  const adapterRegistry = getAdapterRegistry();
  if (version) {
    return adapterRegistry.get(adapterKey(providerKey, version)) ?? adapterRegistry.get(providerKey);
  }
  return adapterRegistry.get(providerKey);
}
function listGatewayAdapters() {
  const adapterRegistry = getAdapterRegistry();
  const seen = /* @__PURE__ */ new Set();
  const result = [];
  for (const [key, adapter] of adapterRegistry) {
    if (!key.includes(":") && !seen.has(adapter.providerKey)) {
      seen.add(adapter.providerKey);
      result.push(adapter);
    }
  }
  return result;
}
function clearGatewayAdapters() {
  getAdapterRegistry().clear();
}
function registerWebhookHandler(providerKey, handler, options) {
  if (options?.maxBodyBytes !== void 0 && (!Number.isSafeInteger(options.maxBodyBytes) || options.maxBodyBytes <= 0 || options.maxBodyBytes > DEFAULT_WEBHOOK_BODY_LIMIT_BYTES)) {
    throw new Error(
      `[internal] Payment gateway webhook maxBodyBytes must be a positive safe integer no greater than ${DEFAULT_WEBHOOK_BODY_LIMIT_BYTES}`
    );
  }
  const webhookHandlerRegistry = getWebhookHandlerRegistry();
  webhookHandlerRegistry.set(providerKey, {
    handler,
    queue: options?.queue,
    readSessionIdHint: options?.readSessionIdHint,
    maxBodyBytes: options?.maxBodyBytes
  });
  return () => {
    webhookHandlerRegistry.delete(providerKey);
  };
}
function getWebhookHandler(providerKey) {
  return getWebhookHandlerRegistry().get(providerKey);
}
function clearWebhookHandlers() {
  getWebhookHandlerRegistry().clear();
}
function registerPaymentGatewayDescriptor(descriptor) {
  const descriptorRegistry = getDescriptorRegistry();
  descriptorRegistry.set(descriptor.providerKey, descriptor);
  return () => {
    descriptorRegistry.delete(descriptor.providerKey);
  };
}
function getPaymentGatewayDescriptor(providerKey) {
  return getDescriptorRegistry().get(providerKey);
}
function listPaymentGatewayDescriptors() {
  return Array.from(getDescriptorRegistry().values()).sort((left, right) => left.label.localeCompare(right.label));
}
function clearPaymentGatewayDescriptors() {
  getDescriptorRegistry().clear();
}
export {
  clearGatewayAdapters,
  clearPaymentGatewayDescriptors,
  clearWebhookHandlers,
  getGatewayAdapter,
  getPaymentGatewayDescriptor,
  getWebhookHandler,
  listGatewayAdapters,
  listPaymentGatewayDescriptors,
  registerGatewayAdapter,
  registerPaymentGatewayDescriptor,
  registerWebhookHandler
};
//# sourceMappingURL=types.js.map
