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,
  formatLineNum,
  DiffLine,
  HunkSeparator
} from "./diff-shared.js";
function DiffPreview({ data, header }) {
  const { unified, filename } = 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));
  const headerText = header ?? data.title ?? "Update file";
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [
    /* @__PURE__ */ jsx(Box, { marginBottom: 0, children: /* @__PURE__ */ jsx(Text, { color: "cyan", bold: true, children: headerText }) }),
    /* @__PURE__ */ jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: [
      /* @__PURE__ */ jsx(Box, { marginBottom: 0, children: /* @__PURE__ */ jsx(Text, { children: makeRelativePath(filename) }) }),
      hunks.map((hunk, hunkIndex) => {
        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 (processedIndices.has(lineIndex)) {
              return null;
            }
            const pair = linePairs.get(lineIndex);
            if (pair) {
              processedIndices.add(lineIndex + 1);
              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);
            }
            return /* @__PURE__ */ jsx(
              DiffLine,
              {
                type: line.type,
                lineNum: line.lineNum,
                lineNumWidth,
                content: line.content
              },
              lineIndex
            );
          })
        ] }, hunkIndex);
      })
    ] })
  ] });
}
function CreateFilePreview({ data, header }) {
  const { path, content, lineCount } = data;
  const headerText = header ?? data.title ?? "Create file";
  if (!content) {
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [
      /* @__PURE__ */ jsx(Box, { marginBottom: 0, children: /* @__PURE__ */ jsx(Text, { color: "cyan", bold: true, children: headerText }) }),
      /* @__PURE__ */ jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: [
        /* @__PURE__ */ jsx(Text, { children: makeRelativePath(path) }),
        lineCount && /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
          lineCount,
          " lines"
        ] })
      ] })
    ] });
  }
  const lines = content.split("\n");
  const lineNumWidth = getLineNumWidth(lines.length);
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [
    /* @__PURE__ */ jsx(Box, { marginBottom: 0, children: /* @__PURE__ */ jsx(Text, { color: "cyan", bold: true, children: headerText }) }),
    /* @__PURE__ */ jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: [
      /* @__PURE__ */ jsx(Box, { marginBottom: 0, children: /* @__PURE__ */ jsx(Text, { children: makeRelativePath(path) }) }),
      lines.map((line, index) => /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsx(Text, { color: "gray", children: formatLineNum(index + 1, lineNumWidth) }),
        /* @__PURE__ */ jsxs(Text, { children: [
          "   ",
          line
        ] })
      ] }, index))
    ] })
  ] });
}
export {
  CreateFilePreview,
  DiffPreview
};
