"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var diff_shared_exports = {};
__export(diff_shared_exports, {
  DiffLine: () => DiffLine,
  HunkSeparator: () => HunkSeparator,
  computeWordDiff: () => computeWordDiff,
  findLinePairs: () => findLinePairs,
  formatLineNum: () => formatLineNum,
  getLineNumWidth: () => getLineNumWidth,
  parseUnifiedDiff: () => parseUnifiedDiff
});
module.exports = __toCommonJS(diff_shared_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_ink = require("ink");
var import_diff = require("diff");
function parseUnifiedDiff(unified) {
  const lines = unified.split("\n");
  const hunks = [];
  let currentHunk = null;
  let oldLine = 0;
  let newLine = 0;
  for (const line of lines) {
    if (line.startsWith("---") || line.startsWith("+++") || line.startsWith("Index:")) {
      continue;
    }
    const hunkMatch = line.match(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
    if (hunkMatch) {
      if (currentHunk) {
        hunks.push(currentHunk);
      }
      oldLine = parseInt(hunkMatch[1], 10);
      newLine = parseInt(hunkMatch[3], 10);
      currentHunk = {
        oldStart: oldLine,
        newStart: newLine,
        lines: []
      };
      continue;
    }
    if (!currentHunk) continue;
    if (line.startsWith("+")) {
      currentHunk.lines.push({
        type: "addition",
        content: line.slice(1),
        lineNum: newLine++
      });
    } else if (line.startsWith("-")) {
      currentHunk.lines.push({
        type: "deletion",
        content: line.slice(1),
        lineNum: oldLine++
      });
    } else if (line.startsWith(" ") || line === "") {
      currentHunk.lines.push({
        type: "context",
        content: line.startsWith(" ") ? line.slice(1) : line,
        lineNum: newLine
      });
      oldLine++;
      newLine++;
    }
  }
  if (currentHunk) {
    hunks.push(currentHunk);
  }
  return hunks;
}
function findLinePairs(lines) {
  const pairs = /* @__PURE__ */ new Map();
  for (let i = 0; i < lines.length - 1; i++) {
    const current = lines[i];
    const next = lines[i + 1];
    if (current.type === "deletion" && next.type === "addition") {
      pairs.set(i, { del: current, add: next });
    }
  }
  return pairs;
}
function computeWordDiff(oldStr, newStr) {
  const changes = (0, import_diff.diffWords)(oldStr, newStr);
  const oldParts = [];
  const newParts = [];
  for (const change of changes) {
    if (change.added) {
      newParts.push({ value: change.value, added: true });
    } else if (change.removed) {
      oldParts.push({ value: change.value, removed: true });
    } else {
      oldParts.push({ value: change.value });
      newParts.push({ value: change.value });
    }
  }
  return { oldParts, newParts };
}
function getLineNumWidth(maxLineNum) {
  return Math.max(3, String(maxLineNum).length);
}
function formatLineNum(num, width) {
  return String(num).padStart(width, " ");
}
function DiffLine({ type, lineNum, lineNumWidth, content, wordDiffParts }) {
  const { stdout } = (0, import_ink.useStdout)();
  const terminalWidth = stdout?.columns ?? 80;
  const lineNumStr = formatLineNum(lineNum, lineNumWidth);
  const gutterWidth = lineNumWidth + 3;
  const contentWidth = Math.max(20, terminalWidth - gutterWidth - 4);
  const getColors = () => {
    switch (type) {
      case "deletion":
        return { bg: "#662222", fg: "white", symbol: "-", symbolColor: "red" };
      case "addition":
        return { bg: "#224466", fg: "white", symbol: "+", symbolColor: "blue" };
      default:
        return { bg: void 0, fg: void 0, symbol: " ", symbolColor: void 0 };
    }
  };
  const colors = getColors();
  const renderContent = () => {
    if (wordDiffParts && wordDiffParts.length > 0) {
      return wordDiffParts.map((part, i) => {
        if (type === "deletion" && part.removed) {
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { backgroundColor: "#882222", children: part.value }, i);
        } else if (type === "addition" && part.added) {
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { backgroundColor: "#224488", children: part.value }, i);
        }
        return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: part.value }, i);
      });
    }
    return content;
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { width: gutterWidth, flexShrink: 0, children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: lineNumStr }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: colors.symbolColor, children: [
        " ",
        colors.symbol,
        " "
      ] })
    ] }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { width: contentWidth, flexShrink: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { backgroundColor: colors.bg, color: colors.fg, wrap: "wrap", children: renderContent() }) })
  ] });
}
function HunkSeparator() {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "..." }) });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DiffLine,
  HunkSeparator,
  computeWordDiff,
  findLinePairs,
  formatLineNum,
  getLineNumWidth,
  parseUnifiedDiff
});
