import { jsx, jsxs } from "react/jsx-runtime";
import React from "react";
import { Box, Text } from "ink";
import { makeRelativePath } from "../../utils/messageFormatting.js";
import {
  parseUnifiedDiff,
  findLinePairs,
  computeWordDiff,
  getLineNumWidth,
  DiffLine,
  HunkSeparator
} from "./diff-shared.js";
function DiffRenderer({ data, maxLines = Infinity }) {
  const { unified, filename, additions, deletions } = data;
  const hunks = parseUnifiedDiff(unified);
  let maxLineNum = 1;
  for (const hunk of hunks) {
    for (const line of hunk.lines) {
      maxLineNum = Math.max(maxLineNum, line.lineNum);
    }
  }
  const lineNumWidth = getLineNumWidth(maxLineNum);
  const allLinePairs = hunks.map((hunk) => findLinePairs(hunk.lines));
  let totalLines = 0;
  for (const hunk of hunks) {
    totalLines += hunk.lines.length;
  }
  const shouldTruncate = totalLines > maxLines;
  let linesRendered = 0;
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
    /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: "gray", children: "  \u23BF " }),
      /* @__PURE__ */ jsx(Text, { children: makeRelativePath(filename) }),
      /* @__PURE__ */ jsxs(Text, { color: "green", children: [
        " +",
        additions
      ] }),
      /* @__PURE__ */ jsxs(Text, { color: "red", children: [
        " -",
        deletions
      ] })
    ] }),
    /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingLeft: 2, children: [
      hunks.map((hunk, hunkIndex) => {
        if (shouldTruncate && linesRendered >= maxLines) {
          return null;
        }
        const linePairs = allLinePairs[hunkIndex];
        const processedIndices = /* @__PURE__ */ new Set();
        return /* @__PURE__ */ jsxs(React.Fragment, { children: [
          hunkIndex > 0 && /* @__PURE__ */ jsx(HunkSeparator, {}),
          hunk.lines.map((line, lineIndex) => {
            if (shouldTruncate && linesRendered >= maxLines) {
              return null;
            }
            if (processedIndices.has(lineIndex)) {
              return null;
            }
            const pair = linePairs.get(lineIndex);
            if (pair) {
              processedIndices.add(lineIndex + 1);
              if (shouldTruncate && linesRendered + 2 > maxLines) {
                linesRendered = maxLines;
                return null;
              }
              linesRendered += 2;
              const { oldParts, newParts } = computeWordDiff(
                pair.del.content,
                pair.add.content
              );
              return /* @__PURE__ */ jsxs(React.Fragment, { children: [
                /* @__PURE__ */ jsx(
                  DiffLine,
                  {
                    type: "deletion",
                    lineNum: pair.del.lineNum,
                    lineNumWidth,
                    content: pair.del.content,
                    wordDiffParts: oldParts
                  }
                ),
                /* @__PURE__ */ jsx(
                  DiffLine,
                  {
                    type: "addition",
                    lineNum: pair.add.lineNum,
                    lineNumWidth,
                    content: pair.add.content,
                    wordDiffParts: newParts
                  }
                )
              ] }, lineIndex);
            }
            linesRendered++;
            return /* @__PURE__ */ jsx(
              DiffLine,
              {
                type: line.type,
                lineNum: line.lineNum,
                lineNumWidth,
                content: line.content
              },
              lineIndex
            );
          })
        ] }, hunkIndex);
      }),
      shouldTruncate && /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
        "... +",
        totalLines - maxLines,
        " lines"
      ] })
    ] })
  ] });
}
export {
  DiffRenderer
};
