import { useEffect, useRef, useState } from "react";
import { getPullMergeFix, startPullMergeFix } from "~/fns/mergefix";
import { useAction } from "~/hooks/use-action";
import { Badge, Panel } from "~/components/ui/Primitives";
import { Button } from "~/components/ui/Button";
import { ErrorBanner } from "~/components/ui/Field";
import { cn } from "~/utils/cn";
type MergeFix = Awaited<ReturnType<typeof getPullMergeFix>>;
type PullParams = { owner: string; repo: string; number: number };
const POLL_MS = 2000;
const ACTIVE = new Set(["queued", "running"]);
const STATUS_COPY = {
queued: "Queued",
running: "Analyzing conflicts",
applied: "Fix committed",
suggested: "Suggestions ready",
failed: "Failed",
} as const;
function percent(value: number | undefined): string {
return `${Math.round((value ?? 0) * 100)}%`;
}
/** Polls the latest run while it is active and reports when it settles. */
function useMergeFix(params: PullParams, initial: MergeFix, onSettled: (fix: MergeFix) => void) {
const [fix, setFix] = useState(initial);
const active = fix !== null && ACTIVE.has(fix.status);
const latest = useRef({ params, onSettled });
latest.current = { params, onSettled };
useEffect(() => {
if (!active) return;
const timer = setInterval(async () => {
const next = await getPullMergeFix({ data: latest.current.params }).catch(() => null);
if (!next) return;
setFix(next);
if (!ACTIVE.has(next.status)) latest.current.onSettled(next);
}, POLL_MS);