import {
  getAllIntegrations
} from "@open-mercato/shared/modules/integrations/types";
import {
  getPaymentGatewayDescriptor,
  listPaymentGatewayDescriptors
} from "@open-mercato/shared/modules/payment_gateways/types";
function findGatewayIntegration(providerKey) {
  return getAllIntegrations().find(
    (integration) => integration.hub === "payment_gateways" && integration.providerKey === providerKey
  );
}
async function resolveDescriptor(descriptor, scope, deps) {
  const integration = findGatewayIntegration(descriptor.providerKey);
  if (!integration) {
    return {
      ...descriptor,
      integrationId: null,
      requiresConfiguration: false,
      isConfigured: true,
      configurationStatus: "unmanaged"
    };
  }
  const [credentials, state] = await Promise.all([
    deps.integrationCredentialsService.resolve(integration.id, scope),
    deps.integrationStateService.resolveState(integration.id, scope)
  ]);
  if (!credentials) {
    return {
      ...descriptor,
      integrationId: integration.id,
      requiresConfiguration: true,
      isConfigured: false,
      configurationStatus: "missing_credentials"
    };
  }
  if (!state.isEnabled) {
    return {
      ...descriptor,
      integrationId: integration.id,
      requiresConfiguration: true,
      isConfigured: false,
      configurationStatus: "disabled"
    };
  }
  return {
    ...descriptor,
    integrationId: integration.id,
    requiresConfiguration: true,
    isConfigured: true,
    configurationStatus: "configured"
  };
}
function createPaymentGatewayDescriptorService(deps) {
  return {
    list() {
      return listPaymentGatewayDescriptors();
    },
    get(providerKey) {
      return getPaymentGatewayDescriptor(providerKey) ?? null;
    },
    async listResolved(scope) {
      return Promise.all(
        listPaymentGatewayDescriptors().map((descriptor) => resolveDescriptor(descriptor, scope, deps))
      );
    },
    async getResolved(providerKey, scope) {
      const descriptor = getPaymentGatewayDescriptor(providerKey);
      if (!descriptor) return null;
      return resolveDescriptor(descriptor, scope, deps);
    }
  };
}
export {
  createPaymentGatewayDescriptorService
};
//# sourceMappingURL=descriptor-service.js.map
