"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 FilePreviewRenderer_exports = {};
__export(FilePreviewRenderer_exports, {
  CreateFilePreview: () => CreateFilePreview,
  DiffPreview: () => DiffPreview
});
module.exports = __toCommonJS(FilePreviewRenderer_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 DiffPreview({ data, header }) {
  const { unified, filename } = 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));
  const headerText = header ?? data.title ?? "Update file";
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", marginBottom: 1, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", bold: true, children: headerText }) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: (0, import_messageFormatting.makeRelativePath)(filename) }) }),
      hunks.map((hunk, hunkIndex) => {
        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 (processedIndices.has(lineIndex)) {
              return null;
            }
            const pair = linePairs.get(lineIndex);
            if (pair) {
              processedIndices.add(lineIndex + 1);
              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);
            }
            return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_diff_shared.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__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", marginBottom: 1, children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", bold: true, children: headerText }) }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: (0, import_messageFormatting.makeRelativePath)(path) }),
        lineCount && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
          lineCount,
          " lines"
        ] })
      ] })
    ] });
  }
  const lines = content.split("\n");
  const lineNumWidth = (0, import_diff_shared.getLineNumWidth)(lines.length);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", marginBottom: 1, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", bold: true, children: headerText }) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: (0, import_messageFormatting.makeRelativePath)(path) }) }),
      lines.map((line, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: (0, import_diff_shared.formatLineNum)(index + 1, lineNumWidth) }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
          "   ",
          line
        ] })
      ] }, index))
    ] })
  ] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CreateFilePreview,
  DiffPreview
});
