import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("communication_channels").child({ component: "oauth-client-config" });
async function resolveOAuthClientCredentials(credentialsService, providerKey, scope) {
  if (!credentialsService) return null;
  const integrationId = `channel_${providerKey}`;
  const organizations = [scope.organizationId, null];
  const tried = /* @__PURE__ */ new Set();
  for (const organizationId of organizations) {
    if (tried.has(organizationId)) continue;
    tried.add(organizationId);
    let row = null;
    try {
      row = await credentialsService.resolve(integrationId, {
        tenantId: scope.tenantId,
        organizationId,
        userId: null
      });
    } catch (resolveErr) {
      logger.warn(
        "resolveOAuthClientCredentials credential resolve failed for an org scope",
        { err: resolveErr }
      );
      row = null;
    }
    if (row && typeof row.clientId === "string" && row.clientId.length > 0) {
      return row;
    }
  }
  return null;
}
export {
  resolveOAuthClientCredentials
};
//# sourceMappingURL=oauth-client-config.js.map
