// @bun
import {
  StructuredDiff,
  fetchGitDiff,
  fetchGitDiffHunks,
  init_StructuredDiff,
  init_gitDiff,
  init_overlayContext,
  init_useShortcutDisplay,
  init_useTerminalSize,
  useRegisterOverlay,
  useShortcutDisplay
} from "./chunk-4vz571bb.js";
import"./chunk-14p6wvsq.js";
import"./chunk-8hseq8b1.js";
import"./chunk-ddtxq6k5.js";
import"./chunk-28jd8qjx.js";
import"./chunk-djt39ze3.js";
import"./chunk-ptxteaeh.js";
import"./chunk-wd53a7m4.js";
import"./chunk-hms8ge0k.js";
import"./chunk-whpphypt.js";
import"./chunk-pht800m5.js";
import"./chunk-pjkqh1kq.js";
import"./chunk-9a9g5hbj.js";
import"./chunk-9hn8e6h1.js";
import"./chunk-rgyzsbs3.js";
import"./chunk-qn6me9n1.js";
import"./chunk-qd3r2s2n.js";
import"./chunk-djzd7tef.js";
import"./chunk-gyhnpbkx.js";
import"./chunk-71sdcaq6.js";
import"./chunk-60gf7p5s.js";
import"./chunk-sj3d0nqx.js";
import"./chunk-9pk9rk2w.js";
import"./chunk-javpza6a.js";
import"./chunk-6v7bre98.js";
import"./chunk-24kv69g3.js";
import"./chunk-fcqebw9m.js";
import"./chunk-dnfd3c9s.js";
import"./chunk-15ezj4ww.js";
import"./chunk-1h284c6e.js";
import"./chunk-582g9yce.js";
import {
  init_useKeybinding
} from "./chunk-y3ztgztb.js";
import"./chunk-4spgkgr3.js";
import"./chunk-n8kbhswc.js";
import"./chunk-60fkafk2.js";
import"./chunk-fgbkrbk3.js";
import"./chunk-ssw61jfd.js";
import"./chunk-093ej2sf.js";
import"./chunk-7tfdhkpy.js";
import"./chunk-nde5ym6a.js";
import"./chunk-k8wq4qsj.js";
import"./chunk-g7d3xx1p.js";
import"./chunk-3wrx2943.js";
import"./chunk-7p9kkh0f.js";
import"./chunk-qp8cgz66.js";
import"./chunk-1mg610h2.js";
import"./chunk-93hc149s.js";
import"./chunk-935nrvdb.js";
import"./chunk-8zz4z1q3.js";
import"./chunk-nnat9jw5.js";
import"./chunk-hqxp6b72.js";
import"./chunk-4jzkqgf1.js";
import"./chunk-33rd41ng.js";
import"./chunk-f257kqw9.js";
import"./chunk-w5hnghah.js";
import"./chunk-36twraxy.js";
import"./chunk-1bkvnj0y.js";
import"./chunk-akdpcc5p.js";
import {
  init_file,
  init_stringUtils,
  plural,
  readFileSafe
} from "./chunk-7rrkrej7.js";
import"./chunk-6r0bxrh6.js";
import"./chunk-d6500tvx.js";
import"./chunk-jg4bw4hp.js";
import"./chunk-epvbnq43.js";
import"./chunk-tn2sx7j6.js";
import"./chunk-6tzyv21c.js";
import"./chunk-8kf8h7xf.js";
import"./chunk-bgan4cpf.js";
import"./chunk-jmv7k0jn.js";
import"./chunk-ch4pgvwq.js";
import"./chunk-99jc4tcw.js";
import"./chunk-fghczfqt.js";
import"./chunk-vwenx8ke.js";
import"./chunk-f86mwfyd.js";
import"./chunk-v4ypszbb.js";
import"./chunk-bk6ck5c2.js";
import"./chunk-ym6j0wv1.js";
import"./chunk-hjmatcgt.js";
import"./chunk-6mdh70q0.js";
import"./chunk-ch92ycde.js";
import"./chunk-e4dsy4g1.js";
import"./chunk-326zehp8.js";
import"./chunk-kc67kt75.js";
import"./chunk-40t1d75v.js";
import"./chunk-nt5dmk8q.js";
import"./chunk-e3abfxpy.js";
import"./chunk-q44zc68f.js";
import"./chunk-fejeqe61.js";
import {
  init_format,
  truncateStartToWidth
} from "./chunk-0gp4q8aq.js";
import"./chunk-hn4w9pkj.js";
import {
  Byline,
  Dialog,
  Divider,
  ThemedBox_default,
  ThemedText,
  init_src,
  require_react,
  useKeybindings,
  useTerminalSize
} from "./chunk-0r6npc3t.js";
import {
  require_jsx_runtime
} from "./chunk-gxyx8b8w.js";
import"./chunk-j1mep9ck.js";
import"./chunk-6x35ffpx.js";
import"./chunk-1zbwhcbt.js";
import"./chunk-t0450ma3.js";
import"./chunk-0cqw3y8h.js";
import"./chunk-k1sw7km0.js";
import"./chunk-e3j7m7k2.js";
import"./chunk-sc9ycxra.js";
import {
  getCwd,
  init_cwd
} from "./chunk-hhta0nge.js";
import"./chunk-8wsj9fff.js";
import"./chunk-m28vg9w4.js";
import"./chunk-f8prtxs3.js";
import"./chunk-41a6f2kv.js";
import"./chunk-c1yc761e.js";
import {
  figures_default,
  init_figures
} from "./chunk-c5g9shkw.js";
import"./chunk-byd20vdq.js";
import"./chunk-kb3758f7.js";
import"./chunk-3j8w0rq8.js";
import"./chunk-tj0dzck2.js";
import"./chunk-aeysytks.js";
import"./chunk-ns1htxgd.js";
import"./chunk-ztqzmfx1.js";
import"./chunk-6k1rsk85.js";
import"./chunk-nxzx0ey9.js";
import"./chunk-yes1my80.js";
import"./chunk-pecy49yr.js";
import"./chunk-azbab59e.js";
import"./chunk-3nk9q8dr.js";
import {
  __esm,
  __toESM
} from "./chunk-hhsxm2yr.js";

// src/hooks/useDiffData.ts
function useDiffData() {
  const [diffResult, setDiffResult] = import_react.useState(null);
  const [hunks, setHunks] = import_react.useState(new Map);
  const [loading, setLoading] = import_react.useState(true);
  import_react.useEffect(() => {
    let cancelled = false;
    async function loadDiffData() {
      try {
        const [statsResult, hunksResult] = await Promise.all([
          fetchGitDiff(),
          fetchGitDiffHunks()
        ]);
        if (!cancelled) {
          setDiffResult(statsResult);
          setHunks(hunksResult);
          setLoading(false);
        }
      } catch (_error) {
        if (!cancelled) {
          setDiffResult(null);
          setHunks(new Map);
          setLoading(false);
        }
      }
    }
    loadDiffData();
    return () => {
      cancelled = true;
    };
  }, []);
  return import_react.useMemo(() => {
    if (!diffResult) {
      return { stats: null, files: [], hunks: new Map, loading };
    }
    const { stats, perFileStats } = diffResult;
    const files = [];
    for (const [path, fileStats] of perFileStats) {
      const fileHunks = hunks.get(path);
      const isUntracked = fileStats.isUntracked ?? false;
      const isLargeFile = !fileStats.isBinary && !isUntracked && !fileHunks;
      const totalLines = fileStats.added + fileStats.removed;
      const isTruncated = !isLargeFile && !fileStats.isBinary && totalLines > MAX_LINES_PER_FILE;
      files.push({
        path,
        linesAdded: fileStats.added,
        linesRemoved: fileStats.removed,
        isBinary: fileStats.isBinary,
        isLargeFile,
        isTruncated,
        isUntracked
      });
    }
    files.sort((a, b) => a.path.localeCompare(b.path));
    return { stats, files, hunks, loading: false };
  }, [diffResult, hunks, loading]);
}
var import_react, MAX_LINES_PER_FILE = 400;
var init_useDiffData = __esm(() => {
  init_gitDiff();
  import_react = __toESM(require_react(), 1);
});

// src/hooks/useTurnDiffs.ts
function isFileEditResult(result) {
  if (!result || typeof result !== "object")
    return false;
  const r = result;
  const hasFilePath = typeof r.filePath === "string";
  const hasStructuredPatch = Array.isArray(r.structuredPatch) && r.structuredPatch.length > 0;
  const isNewFile = r.type === "create" && typeof r.content === "string";
  return hasFilePath && (hasStructuredPatch || isNewFile);
}
function isFileWriteOutput(result) {
  return "type" in result && (result.type === "create" || result.type === "update");
}
function countHunkLines(hunks) {
  let added = 0;
  let removed = 0;
  for (const hunk of hunks) {
    for (const line of hunk.lines) {
      if (line.startsWith("+"))
        added++;
      else if (line.startsWith("-"))
        removed++;
    }
  }
  return { added, removed };
}
function getUserPromptPreview(message) {
  if (message.type !== "user")
    return "";
  const content = message.message.content;
  const text = typeof content === "string" ? content : "";
  if (text.length <= 30)
    return text;
  return text.slice(0, 29) + "\u2026";
}
function computeTurnStats(turn) {
  let totalAdded = 0;
  let totalRemoved = 0;
  for (const file of turn.files.values()) {
    totalAdded += file.linesAdded;
    totalRemoved += file.linesRemoved;
  }
  turn.stats = {
    filesChanged: turn.files.size,
    linesAdded: totalAdded,
    linesRemoved: totalRemoved
  };
}
function useTurnDiffs(messages) {
  const cache = import_react2.useRef({
    completedTurns: [],
    currentTurn: null,
    lastProcessedIndex: 0,
    lastTurnIndex: 0
  });
  return import_react2.useMemo(() => {
    const c = cache.current;
    if (messages.length < c.lastProcessedIndex) {
      c.completedTurns = [];
      c.currentTurn = null;
      c.lastProcessedIndex = 0;
      c.lastTurnIndex = 0;
    }
    for (let i = c.lastProcessedIndex;i < messages.length; i++) {
      const message = messages[i];
      if (!message || message.type !== "user")
        continue;
      const isToolResult = message.toolUseResult || Array.isArray(message.message.content) && message.message.content[0]?.type === "tool_result";
      if (!isToolResult && !message.isMeta) {
        if (c.currentTurn && c.currentTurn.files.size > 0) {
          computeTurnStats(c.currentTurn);
          c.completedTurns.push(c.currentTurn);
        }
        c.lastTurnIndex++;
        c.currentTurn = {
          turnIndex: c.lastTurnIndex,
          userPromptPreview: getUserPromptPreview(message),
          timestamp: message.timestamp,
          files: new Map,
          stats: { filesChanged: 0, linesAdded: 0, linesRemoved: 0 }
        };
      } else if (c.currentTurn && message.toolUseResult) {
        const result2 = message.toolUseResult;
        if (isFileEditResult(result2)) {
          const { filePath, structuredPatch } = result2;
          const isNewFile = "type" in result2 && result2.type === "create";
          let fileEntry = c.currentTurn.files.get(filePath);
          if (!fileEntry) {
            fileEntry = {
              filePath,
              hunks: [],
              isNewFile,
              linesAdded: 0,
              linesRemoved: 0
            };
            c.currentTurn.files.set(filePath, fileEntry);
          }
          if (isNewFile && structuredPatch.length === 0 && isFileWriteOutput(result2)) {
            const content = result2.content;
            const lines = content.split(`
`);
            const syntheticHunk = {
              oldStart: 0,
              oldLines: 0,
              newStart: 1,
              newLines: lines.length,
              lines: lines.map((l) => "+" + l)
            };
            fileEntry.hunks.push(syntheticHunk);
            fileEntry.linesAdded += lines.length;
          } else {
            fileEntry.hunks.push(...structuredPatch);
            const { added, removed } = countHunkLines(structuredPatch);
            fileEntry.linesAdded += added;
            fileEntry.linesRemoved += removed;
          }
          if (isNewFile) {
            fileEntry.isNewFile = true;
          }
        }
      }
    }
    c.lastProcessedIndex = messages.length;
    const result = [...c.completedTurns];
    if (c.currentTurn && c.currentTurn.files.size > 0) {
      computeTurnStats(c.currentTurn);
      result.push(c.currentTurn);
    }
    return result.reverse();
  }, [messages]);
}
var import_react2;
var init_useTurnDiffs = __esm(() => {
  import_react2 = __toESM(require_react(), 1);
});

// src/components/diff/DiffDetailView.tsx
import { resolve } from "path";
function DiffDetailView({
  filePath,
  hunks,
  isLargeFile,
  isBinary,
  isTruncated,
  isUntracked
}) {
  const { columns } = useTerminalSize();
  const { firstLine, fileContent } = import_react3.useMemo(() => {
    if (!filePath) {
      return { firstLine: null, fileContent: undefined };
    }
    const fullPath = resolve(getCwd(), filePath);
    const content = readFileSafe(fullPath);
    return {
      firstLine: content?.split(`
`)[0] ?? null,
      fileContent: content ?? undefined
    };
  }, [filePath]);
  if (isUntracked) {
    return /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
      flexDirection: "column",
      width: "100%",
      children: [
        /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
          children: [
            /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
              bold: true,
              children: filePath
            }),
            /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
              dimColor: true,
              children: " (untracked)"
            })
          ]
        }),
        /* @__PURE__ */ jsx_runtime.jsx(Divider, {
          padding: 4
        }),
        /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
          flexDirection: "column",
          children: [
            /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
              dimColor: true,
              italic: true,
              children: "New file not yet staged."
            }),
            /* @__PURE__ */ jsx_runtime.jsxs(ThemedText, {
              dimColor: true,
              italic: true,
              children: [
                "Run `git add ",
                filePath,
                "` to see line counts."
              ]
            })
          ]
        })
      ]
    });
  }
  if (isBinary) {
    return /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
      flexDirection: "column",
      width: "100%",
      children: [
        /* @__PURE__ */ jsx_runtime.jsx(ThemedBox_default, {
          children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            bold: true,
            children: filePath
          })
        }),
        /* @__PURE__ */ jsx_runtime.jsx(Divider, {
          padding: 4
        }),
        /* @__PURE__ */ jsx_runtime.jsx(ThemedBox_default, {
          flexDirection: "column",
          children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            dimColor: true,
            italic: true,
            children: "Binary file - cannot display diff"
          })
        })
      ]
    });
  }
  if (isLargeFile) {
    return /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
      flexDirection: "column",
      width: "100%",
      children: [
        /* @__PURE__ */ jsx_runtime.jsx(ThemedBox_default, {
          children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            bold: true,
            children: filePath
          })
        }),
        /* @__PURE__ */ jsx_runtime.jsx(Divider, {
          padding: 4
        }),
        /* @__PURE__ */ jsx_runtime.jsx(ThemedBox_default, {
          flexDirection: "column",
          children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            dimColor: true,
            italic: true,
            children: "Large file - diff exceeds 1 MB limit"
          })
        })
      ]
    });
  }
  const outerPaddingX = 1;
  const outerBorderWidth = 1;
  return /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
    flexDirection: "column",
    width: "100%",
    children: [
      /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
        children: [
          /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            bold: true,
            children: filePath
          }),
          isTruncated && /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            dimColor: true,
            children: " (truncated)"
          })
        ]
      }),
      /* @__PURE__ */ jsx_runtime.jsx(Divider, {
        padding: 4
      }),
      /* @__PURE__ */ jsx_runtime.jsx(ThemedBox_default, {
        flexDirection: "column",
        children: hunks.length === 0 ? /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
          dimColor: true,
          children: "No diff content"
        }) : hunks.map((hunk, index) => /* @__PURE__ */ jsx_runtime.jsx(StructuredDiff, {
          patch: hunk,
          filePath,
          firstLine,
          fileContent,
          dim: false,
          width: columns - 2 * outerPaddingX - 2 * outerBorderWidth
        }, index))
      }),
      isTruncated && /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
        dimColor: true,
        italic: true,
        children: "\u2026 diff truncated (exceeded 400 line limit)"
      })
    ]
  });
}
var import_react3, jsx_runtime;
var init_DiffDetailView = __esm(() => {
  init_useTerminalSize();
  init_src();
  init_cwd();
  init_file();
  init_src();
  init_StructuredDiff();
  import_react3 = __toESM(require_react(), 1);
  jsx_runtime = __toESM(require_jsx_runtime(), 1);
});

// src/components/diff/DiffFileList.tsx
function DiffFileList({ files, selectedIndex }) {
  const { columns } = useTerminalSize();
  const { startIndex, endIndex } = import_react4.useMemo(() => {
    if (files.length === 0 || files.length <= MAX_VISIBLE_FILES) {
      return { startIndex: 0, endIndex: files.length };
    }
    let start = Math.max(0, selectedIndex - Math.floor(MAX_VISIBLE_FILES / 2));
    let end = start + MAX_VISIBLE_FILES;
    if (end > files.length) {
      end = files.length;
      start = Math.max(0, end - MAX_VISIBLE_FILES);
    }
    return { startIndex: start, endIndex: end };
  }, [files.length, selectedIndex]);
  if (files.length === 0) {
    return /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
      dimColor: true,
      children: "No changed files"
    });
  }
  const visibleFiles = files.slice(startIndex, endIndex);
  const hasMoreAbove = startIndex > 0;
  const hasMoreBelow = endIndex < files.length;
  const needsPagination = files.length > MAX_VISIBLE_FILES;
  const statsWidth = 16;
  const pointerWidth = 3;
  const maxPathWidth = Math.max(20, columns - statsWidth - pointerWidth - 4);
  return /* @__PURE__ */ jsx_runtime2.jsxs(ThemedBox_default, {
    flexDirection: "column",
    children: [
      needsPagination && /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
        dimColor: true,
        children: hasMoreAbove ? ` \u2191 ${startIndex} more ${plural(startIndex, "file")}` : " "
      }),
      visibleFiles.map((file, index) => /* @__PURE__ */ jsx_runtime2.jsx(FileItem, {
        file,
        isSelected: startIndex + index === selectedIndex,
        maxPathWidth
      }, file.path)),
      needsPagination && /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
        dimColor: true,
        children: hasMoreBelow ? ` \u2193 ${files.length - endIndex} more ${plural(files.length - endIndex, "file")}` : " "
      })
    ]
  });
}
function FileItem({
  file,
  isSelected,
  maxPathWidth
}) {
  const displayPath = truncateStartToWidth(file.path, maxPathWidth);
  const pointer = isSelected ? figures_default.pointer + " " : "  ";
  const line = `${pointer}${displayPath}`;
  return /* @__PURE__ */ jsx_runtime2.jsxs(ThemedBox_default, {
    flexDirection: "row",
    children: [
      /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
        bold: isSelected,
        color: isSelected ? "background" : undefined,
        inverse: isSelected,
        children: line
      }),
      /* @__PURE__ */ jsx_runtime2.jsx(ThemedBox_default, {
        flexGrow: 1
      }),
      /* @__PURE__ */ jsx_runtime2.jsx(FileStats, {
        file,
        isSelected
      })
    ]
  });
}
function FileStats({ file, isSelected }) {
  if (file.isUntracked) {
    return /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
      dimColor: !isSelected,
      italic: true,
      children: "untracked"
    });
  }
  if (file.isBinary) {
    return /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
      dimColor: !isSelected,
      italic: true,
      children: "Binary file"
    });
  }
  if (file.isLargeFile) {
    return /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
      dimColor: !isSelected,
      italic: true,
      children: "Large file modified"
    });
  }
  return /* @__PURE__ */ jsx_runtime2.jsxs(ThemedText, {
    children: [
      file.linesAdded > 0 && /* @__PURE__ */ jsx_runtime2.jsxs(ThemedText, {
        color: "diffAddedWord",
        bold: isSelected,
        children: [
          "+",
          file.linesAdded
        ]
      }),
      file.linesAdded > 0 && file.linesRemoved > 0 && " ",
      file.linesRemoved > 0 && /* @__PURE__ */ jsx_runtime2.jsxs(ThemedText, {
        color: "diffRemovedWord",
        bold: isSelected,
        children: [
          "-",
          file.linesRemoved
        ]
      }),
      file.isTruncated && /* @__PURE__ */ jsx_runtime2.jsx(ThemedText, {
        dimColor: !isSelected,
        children: " (truncated)"
      })
    ]
  });
}
var import_react4, jsx_runtime2, MAX_VISIBLE_FILES = 5;
var init_DiffFileList = __esm(() => {
  init_figures();
  init_useTerminalSize();
  init_src();
  init_format();
  init_stringUtils();
  import_react4 = __toESM(require_react(), 1);
  jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
});

// src/components/diff/DiffDialog.tsx
function turnDiffToDiffData(turn) {
  const files = Array.from(turn.files.values()).map((f) => ({
    path: f.filePath,
    linesAdded: f.linesAdded,
    linesRemoved: f.linesRemoved,
    isBinary: false,
    isLargeFile: false,
    isTruncated: false,
    isNewFile: f.isNewFile
  })).sort((a, b) => a.path.localeCompare(b.path));
  const hunks = new Map;
  for (const f of turn.files.values()) {
    hunks.set(f.filePath, f.hunks);
  }
  return {
    stats: {
      filesCount: turn.stats.filesChanged,
      linesAdded: turn.stats.linesAdded,
      linesRemoved: turn.stats.linesRemoved
    },
    files,
    hunks,
    loading: false
  };
}
function DiffDialog({ messages, onDone }) {
  const gitDiffData = useDiffData();
  const turnDiffs = useTurnDiffs(messages);
  const [viewMode, setViewMode] = import_react5.useState("list");
  const [selectedIndex, setSelectedIndex] = import_react5.useState(0);
  const [sourceIndex, setSourceIndex] = import_react5.useState(0);
  const sources = import_react5.useMemo(() => [{ type: "current" }, ...turnDiffs.map((turn) => ({ type: "turn", turn }))], [turnDiffs]);
  const currentSource = sources[sourceIndex];
  const currentTurn = currentSource?.type === "turn" ? currentSource.turn : null;
  const diffData = import_react5.useMemo(() => {
    return currentTurn ? turnDiffToDiffData(currentTurn) : gitDiffData;
  }, [currentTurn, gitDiffData]);
  const selectedFile = diffData.files[selectedIndex];
  const selectedHunks = import_react5.useMemo(() => {
    return selectedFile ? diffData.hunks.get(selectedFile.path) || [] : [];
  }, [selectedFile, diffData.hunks]);
  import_react5.useEffect(() => {
    if (sourceIndex >= sources.length) {
      setSourceIndex(Math.max(0, sources.length - 1));
    }
  }, [sources.length, sourceIndex]);
  const prevSourceIndex = import_react5.useRef(sourceIndex);
  import_react5.useEffect(() => {
    if (prevSourceIndex.current !== sourceIndex) {
      setSelectedIndex(0);
      prevSourceIndex.current = sourceIndex;
    }
  }, [sourceIndex]);
  useRegisterOverlay("diff-dialog");
  useKeybindings({
    "diff:previousSource": () => {
      if (viewMode === "detail") {
        setViewMode("list");
      } else if (viewMode === "list" && sources.length > 1) {
        setSourceIndex((prev) => Math.max(0, prev - 1));
      }
    },
    "diff:nextSource": () => {
      if (viewMode === "list" && sources.length > 1) {
        setSourceIndex((prev) => Math.min(sources.length - 1, prev + 1));
      }
    },
    "diff:back": () => {
      if (viewMode === "detail") {
        setViewMode("list");
      }
    },
    "diff:viewDetails": () => {
      if (viewMode === "list" && selectedFile) {
        setViewMode("detail");
      }
    },
    "diff:previousFile": () => {
      if (viewMode === "list") {
        setSelectedIndex((prev) => Math.max(0, prev - 1));
      }
    },
    "diff:nextFile": () => {
      if (viewMode === "list") {
        setSelectedIndex((prev) => Math.min(diffData.files.length - 1, prev + 1));
      }
    }
  }, { context: "DiffDialog" });
  const subtitle = diffData.stats ? /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
    dimColor: true,
    children: [
      diffData.stats.filesCount,
      " ",
      plural(diffData.stats.filesCount, "file"),
      " changed",
      diffData.stats.linesAdded > 0 && /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
        color: "diffAddedWord",
        children: [
          " +",
          diffData.stats.linesAdded
        ]
      }),
      diffData.stats.linesRemoved > 0 && /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
        color: "diffRemovedWord",
        children: [
          " -",
          diffData.stats.linesRemoved
        ]
      })
    ]
  }) : null;
  const headerTitle = currentTurn ? `Turn ${currentTurn.turnIndex}` : "Uncommitted changes";
  const headerSubtitle = currentTurn ? currentTurn.userPromptPreview ? `"${currentTurn.userPromptPreview}"` : "" : "(git diff HEAD)";
  const sourceSelector = sources.length > 1 ? /* @__PURE__ */ jsx_runtime3.jsxs(ThemedBox_default, {
    children: [
      sourceIndex > 0 && /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
        dimColor: true,
        children: "\u25C0 "
      }),
      sources.map((source, i) => {
        const isSelected = i === sourceIndex;
        const label = source.type === "current" ? "Current" : `T${source.turn.turnIndex}`;
        return /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
          dimColor: !isSelected,
          bold: isSelected,
          children: [
            i > 0 ? " \xB7 " : "",
            label
          ]
        }, i);
      }),
      sourceIndex < sources.length - 1 && /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
        dimColor: true,
        children: " \u25B6"
      })
    ]
  }) : null;
  const dismissShortcut = useShortcutDisplay("diff:dismiss", "DiffDialog", "esc");
  const emptyMessage = (() => {
    if (diffData.loading) {
      return "Loading diff\u2026";
    }
    if (currentTurn) {
      return "No file changes in this turn";
    }
    if (diffData.stats && diffData.stats.filesCount > 0 && diffData.files.length === 0) {
      return "Too many files to display details";
    }
    return "Working tree is clean";
  })();
  const title = /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
    children: [
      headerTitle,
      headerSubtitle && /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
        dimColor: true,
        children: [
          " ",
          headerSubtitle
        ]
      })
    ]
  });
  function handleCancel() {
    if (viewMode === "detail") {
      setViewMode("list");
    } else {
      onDone("Diff dialog dismissed", { display: "system" });
    }
  }
  return /* @__PURE__ */ jsx_runtime3.jsxs(Dialog, {
    title,
    onCancel: handleCancel,
    color: "background",
    inputGuide: (exitState) => exitState.pending ? /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
      children: [
        "Press ",
        exitState.keyName,
        " again to exit"
      ]
    }) : viewMode === "list" ? /* @__PURE__ */ jsx_runtime3.jsxs(Byline, {
      children: [
        sources.length > 1 && /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
          children: "\u2190/\u2192 source"
        }),
        /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
          children: "\u2191/\u2193 select"
        }),
        /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
          children: "Enter view"
        }),
        /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
          children: [
            dismissShortcut,
            " close"
          ]
        })
      ]
    }) : /* @__PURE__ */ jsx_runtime3.jsxs(Byline, {
      children: [
        /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
          children: "\u2190 back"
        }),
        /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
          children: [
            dismissShortcut,
            " close"
          ]
        })
      ]
    }),
    children: [
      sourceSelector,
      subtitle,
      diffData.files.length === 0 ? /* @__PURE__ */ jsx_runtime3.jsx(ThemedBox_default, {
        marginTop: 1,
        children: /* @__PURE__ */ jsx_runtime3.jsx(ThemedText, {
          dimColor: true,
          children: emptyMessage
        })
      }) : viewMode === "list" ? /* @__PURE__ */ jsx_runtime3.jsx(ThemedBox_default, {
        flexDirection: "column",
        marginTop: 1,
        children: /* @__PURE__ */ jsx_runtime3.jsx(DiffFileList, {
          files: diffData.files,
          selectedIndex
        })
      }) : /* @__PURE__ */ jsx_runtime3.jsx(ThemedBox_default, {
        flexDirection: "column",
        marginTop: 1,
        children: /* @__PURE__ */ jsx_runtime3.jsx(DiffDetailView, {
          filePath: selectedFile?.path || "",
          hunks: selectedHunks,
          isLargeFile: selectedFile?.isLargeFile,
          isBinary: selectedFile?.isBinary,
          isTruncated: selectedFile?.isTruncated,
          isUntracked: selectedFile?.isUntracked
        })
      })
    ]
  });
}
var import_react5, jsx_runtime3;
var init_DiffDialog = __esm(() => {
  init_overlayContext();
  init_useDiffData();
  init_useTurnDiffs();
  init_src();
  init_useKeybinding();
  init_useShortcutDisplay();
  init_stringUtils();
  init_src();
  init_DiffDetailView();
  init_DiffFileList();
  import_react5 = __toESM(require_react(), 1);
  jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
});
init_DiffDialog();

export {
  DiffDialog
};

//# debugId=363EAC503C8850DA64756E2164756E21
//# sourceMappingURL=chunk-gn4pfwmg.js.map
