import { env } from "cloudflare:workers";
import { all, chunks, first, placeholders, run } from "../db";
import { sha256Hex } from "../crypto";
import { loadRepoById } from "../repos";
import { badRequest, notFoundError } from "../errors";
import { publicUrl } from "../events/emit";
import { deliverToIntegration } from "./deliver";
import type { DeliverInput } from "./deliver";
import { isPermanentFailure, isSuccess } from "./outbound";
import { failImportJob, runImportPage } from "./github-import";
import type { ImportJobMessage } from "./github-import";
import { integrationsForRepo, loadIntegration, parseConfig, parseEvents, recordDelivery } from "./store";
import type { IntegrationRow } from "./store";
import { passesFilters, summarizeEvent } from "./summarize";
import type { EventSummary, RepoContext } from "./summarize";
import type { RepoRow } from "~/lib/types";
export const INTEGRATIONS_QUEUE = "gitfor-integrations";
export const INTEGRATIONS_DLQ = "gitfor-integrations-dlq";
const BASE_RETRY_SECONDS = 15;
const MAX_RETRY_SECONDS = 3600;
const MAX_QUEUED_PAYLOAD = 96 * 1024;
const MAX_PAYLOAD_COMMITS = 20;
const DIGEST_BATCH = 25;
const DIGEST_ITEMS = 80;
const MAX_DIGEST_BACKLOG = 500;
const MIRROR_BATCH = 10;
const ID_LENGTH = 20;
const MAX_BATCH_BYTES = 200 * 1024;
const MAX_BATCH_MESSAGES = 50;
const MIRROR_INTERVAL_MS = 15 * 60_000;
export type IntegrationMessage =
| { kind: "integration.dispatch"; repoId: string; event: string; payload: Record<string, unknown> }
| ({ kind: "integration.deliver"; integrationId: string } & DeliverInput)
| ImportJobMessage;
/** Fans a forge event out to integrations. Called from the events consumer for every dispatched webhook. */
export async function enqueueIntegrationDispatch(