"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var DiffRenderer_exports = {};
__export(DiffRenderer_exports, {
  DiffRenderer: () => DiffRenderer
});
module.exports = __toCommonJS(DiffRenderer_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"), 1);
var import_ink = require("ink");
var import_messageFormatting = require("../../utils/messageFormatting.js");
var import_diff_shared = require("./diff-shared.js");
function DiffRenderer({ data, maxLines = Infinity }) {
  const { unified, filename, additions, deletions } = data;
  const hunks = (0, import_diff_shared.parseUnifiedDiff)(unified);
  let maxLineNum = 1;
  for (const hunk of hunks) {
    for (const line of hunk.lines) {
      maxLineNum = Math.max(maxLineNum, line.lineNum);
    }
  }
  const lineNumWidth = (0, import_diff_shared.getLineNumWidth)(maxLineNum);
  const allLinePairs = hunks.map((hunk) => (0, import_diff_shared.findLinePairs)(hunk.lines));
  let totalLines = 0;
  for (const hunk of hunks) {
    totalLines += hunk.lines.length;
  }
  const shouldTruncate = totalLines > maxLines;
  let linesRendered = 0;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "  \u23BF " }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: (0, import_messageFormatting.makeRelativePath)(filename) }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "green", children: [
        " +",
        additions
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "red", children: [
        " -",
        deletions
      ] })
    ] }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.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__ */ (0, import_jsx_runtime.jsxs)(import_react.default.Fragment, { children: [
          hunkIndex > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_diff_shared.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 } = (0, import_diff_shared.computeWordDiff)(
                pair.del.content,
                pair.add.content
              );
              return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react.default.Fragment, { children: [
                /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                  import_diff_shared.DiffLine,
                  {
                    type: "deletion",
                    lineNum: pair.del.lineNum,
                    lineNumWidth,
                    content: pair.del.content,
                    wordDiffParts: oldParts
                  }
                ),
                /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                  import_diff_shared.DiffLine,
                  {
                    type: "addition",
                    lineNum: pair.add.lineNum,
                    lineNumWidth,
                    content: pair.add.content,
                    wordDiffParts: newParts
                  }
                )
              ] }, lineIndex);
            }
            linesRendered++;
            return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_diff_shared.DiffLine,
              {
                type: line.type,
                lineNum: line.lineNum,
                lineNumWidth,
                content: line.content
              },
              lineIndex
            );
          })
        ] }, hunkIndex);
      }),
      shouldTruncate && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
        "... +",
        totalLines - maxLines,
        " lines"
      ] })
    ] })
  ] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DiffRenderer
});
