import { getAllIntegrations } from "@open-mercato/shared/modules/integrations/types";
import { getEffectiveHealthCheckConfig } from "../lib/health-service.js";
const CHUNK = 5;
const metadata = {
  queue: "integration-health-probe",
  id: "integrations:health-probe",
  concurrency: 5
};
async function mapInChunks(items, size, fn) {
  const out = [];
  for (let index = 0; index < items.length; index += size) {
    const slice = items.slice(index, index + size);
    const batch = await Promise.all(slice.map((item) => fn(item)));
    out.push(...batch);
  }
  return out;
}
async function handle(job, ctx) {
  const scope = job.payload.scope ?? {
    organizationId: job.payload.organizationId,
    tenantId: job.payload.tenantId
  };
  if (!scope.organizationId || !scope.tenantId) {
    return;
  }
  const stateService = ctx.resolve("integrationStateService");
  const healthService = ctx.resolve("integrationHealthService");
  const candidates = [];
  for (const definition of getAllIntegrations()) {
    const enabled = await stateService.isEnabled(definition.id, scope);
    if (!enabled) continue;
    const healthConfig = getEffectiveHealthCheckConfig(definition.id);
    if (!healthConfig?.service) continue;
    candidates.push(definition.id);
  }
  await mapInChunks(candidates, CHUNK, async (integrationId) => {
    await healthService.runHealthCheck(integrationId, scope);
  });
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=health-probe.js.map
