import { ClientOnly } from "@tanstack/react-router";
import { parsePatchFiles } from "@pierre/diffs";
import type { DiffLineAnnotation, FileDiffMetadata, SelectedLineRange } from "@pierre/diffs";
import { FileDiff } from "@pierre/diffs/react";
import { useState } from "react";
import { Button } from "~/ui/Button";
import { EmptyState } from "~/ui/EmptyState";
import { ImageDiff, isImagePath } from "./rich/ImageDiff";
import type { ImageSources } from "./rich/ImageDiff";
import type { DiffResult } from "~/lib/types";
import { Callout } from "~/ui/Callout";
import { cn } from "~/utils/cn";
export type DiffSide = "LEFT" | "RIGHT";
export type LineAnchor<T> = { path: string; line: number; side: DiffSide; data: T };
/** First line of a multi-line selection; the anchor line is its last line. */
export type RangeStart = { line: number; side: DiffSide };
type LineAction = (path: string, line: number, side: DiffSide, start?: RangeStart) => void;
function selectionAction(path: string, range: SelectedLineRange, onLineAction: LineAction) {
const startSide: DiffSide = range.side === "deletions" ? "LEFT" : "RIGHT";
const endSide: DiffSide = (range.endSide ?? range.side) === "deletions" ? "LEFT" : "RIGHT";
if (startSide !== endSide || range.start === range.end) {
onLineAction(path, range.end, endSide);
return;
}
const [first, last] = range.start < range.end ? [range.start, range.end] : [range.end, range.start];
onLineAction(path, last, endSide, { line: first, side: startSide });
}
type Props<T> = {
diff: DiffResult;
anchors?: LineAnchor<T>[];
renderAnchor?: (anchor: LineAnchor<T>) => React.ReactNode;
onLineAction?: LineAction;
images?: ImageSources;
};