"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 CommandOutputOverlay_exports = {};
__export(CommandOutputOverlay_exports, {
  CommandOutputOverlay: () => CommandOutputOverlay
});
module.exports = __toCommonJS(CommandOutputOverlay_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_wrap_ansi = __toESM(require("wrap-ansi"), 1);
var import_useTerminalSize = require("../../hooks/useTerminalSize.js");
var import_overlaySizing = require("../../utils/overlaySizing.js");
var import_HintBar = require("../shared/HintBar.js");
const CommandOutputOverlay = (0, import_react.forwardRef)(function CommandOutputOverlay2({ isVisible, title, content, onClose }, ref) {
  const { columns, rows } = (0, import_useTerminalSize.useTerminalSize)();
  const viewportLines = (0, import_react.useMemo)(() => {
    return (0, import_overlaySizing.getMaxVisibleItemsForTerminalRows)({
      rows,
      hardCap: 20,
      reservedRows: 10
    });
  }, [rows]);
  const [scrollOffset, setScrollOffset] = (0, import_react.useState)(0);
  const wrappedLines = (0, import_react.useMemo)(() => {
    const width = Math.max(20, columns - 2);
    const wrapped = (0, import_wrap_ansi.default)(content || "", width, {
      hard: true,
      wordWrap: true,
      trim: false
    });
    return wrapped.length > 0 ? wrapped.split("\n") : [];
  }, [content, columns]);
  const maxScrollOffset = (0, import_react.useMemo)(() => {
    return Math.max(0, wrappedLines.length - viewportLines);
  }, [wrappedLines.length, viewportLines]);
  (0, import_react.useEffect)(() => {
    setScrollOffset((prev) => Math.min(maxScrollOffset, Math.max(0, prev)));
  }, [maxScrollOffset]);
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      handleInput: (_input, key) => {
        if (!isVisible) return false;
        if (key.escape) {
          onClose();
          return true;
        }
        if (key.upArrow) {
          setScrollOffset((prev) => Math.max(0, prev - 1));
          return true;
        }
        if (key.downArrow) {
          setScrollOffset((prev) => Math.min(maxScrollOffset, prev + 1));
          return true;
        }
        return true;
      }
    }),
    [isVisible, maxScrollOffset, onClose]
  );
  if (!isVisible) return null;
  const visible = wrappedLines.slice(scrollOffset, scrollOffset + viewportLines);
  const hint = wrappedLines.length > viewportLines ? ["\u2191\u2193 scroll", "Esc close"] : ["Esc close"];
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", width: columns, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 0, paddingY: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", bold: true, wrap: "truncate-end", children: title }) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { flexDirection: "column", height: viewportLines, marginTop: 1, children: Array.from({ length: viewportLines }, (_, rowIndex) => {
      const line = visible[rowIndex];
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 0, paddingY: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { wrap: "truncate-end", children: line ?? " " }) }, `line-${rowIndex}`);
    }) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 0, paddingY: 0, marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_HintBar.HintBar, { hints: hint }) })
  ] });
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CommandOutputOverlay
});
