// Force strict mode and setup for ESM
"use strict";
import {
  disableMouseEvents,
  enableMouseEvents,
  useKeypressContext
} from "./chunk-RMXPR6HS.js";
import {
  use_stdin_default,
  use_stdout_default
} from "./chunk-4FRUXVSE.js";
import {
  require_react
} from "./chunk-KYMBIKIW.js";
import {
  themeManager
} from "./chunk-HTWHAN44.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name,
  __toESM
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/ui/contexts/SettingsContext.tsx
init_esbuild_shims();
var import_react = __toESM(require_react(), 1);
var SettingsContext = import_react.default.createContext(
  void 0
);
var useSettings = /* @__PURE__ */ __name(() => {
  const context = (0, import_react.useContext)(SettingsContext);
  if (context === void 0) {
    throw new Error("useSettings must be used within a SettingsProvider");
  }
  return context;
}, "useSettings");

// packages/cli/src/ui/hooks/useTerminalSize.ts
init_esbuild_shims();
var import_react2 = __toESM(require_react(), 1);
function useTerminalSize() {
  const [size, setSize] = (0, import_react2.useState)({
    columns: process.stdout.columns || 80,
    rows: process.stdout.rows || 24
  });
  (0, import_react2.useEffect)(() => {
    function updateSize() {
      setSize({
        columns: process.stdout.columns || 80,
        rows: process.stdout.rows || 24
      });
    }
    __name(updateSize, "updateSize");
    process.stdout.on("resize", updateSize);
    return () => {
      process.stdout.off("resize", updateSize);
    };
  }, []);
  return size;
}
__name(useTerminalSize, "useTerminalSize");

// packages/cli/src/ui/hooks/useMouseEvents.ts
init_esbuild_shims();
var import_react3 = __toESM(require_react(), 1);
var mouseModeRefs = /* @__PURE__ */ new Map();
function effectiveTracking(entry) {
  if (entry.any > 0) return "any";
  if (entry.button > 0) return "button";
  return null;
}
__name(effectiveTracking, "effectiveTracking");
function reconcileMouseMode(stdout, entry) {
  const desired = effectiveTracking(entry);
  if (desired === entry.active) return;
  if (entry.active) disableMouseEvents(stdout, entry.active);
  if (desired) enableMouseEvents(stdout, desired);
  entry.active = desired;
}
__name(reconcileMouseMode, "reconcileMouseMode");
var disableAllMouseModes = /* @__PURE__ */ __name(() => {
  for (const [stdout, entry] of mouseModeRefs) {
    if (entry.active) disableMouseEvents(stdout, entry.active);
  }
  mouseModeRefs.clear();
}, "disableAllMouseModes");
function acquireMouseMode(stdout, tracking) {
  let entry = mouseModeRefs.get(stdout);
  if (!entry) {
    if (mouseModeRefs.size === 0) {
      process.on("exit", disableAllMouseModes);
    }
    entry = { button: 0, any: 0, active: null };
    mouseModeRefs.set(stdout, entry);
  }
  entry[tracking] += 1;
  reconcileMouseMode(stdout, entry);
}
__name(acquireMouseMode, "acquireMouseMode");
function releaseMouseMode(stdout, tracking) {
  const entry = mouseModeRefs.get(stdout);
  if (!entry) return;
  entry[tracking] = Math.max(0, entry[tracking] - 1);
  reconcileMouseMode(stdout, entry);
  if (entry.button === 0 && entry.any === 0) {
    mouseModeRefs.delete(stdout);
    if (mouseModeRefs.size === 0) {
      process.removeListener("exit", disableAllMouseModes);
    }
  }
}
__name(releaseMouseMode, "releaseMouseMode");
function useMouseEvents(handler, { isActive, tracking = "button", bypassVpGate = false }) {
  const { isRawModeSupported } = use_stdin_default();
  const { stdout } = use_stdout_default();
  const { subscribeMouse, unsubscribeMouse } = useKeypressContext();
  const settings = (0, import_react3.useContext)(SettingsContext);
  const isVpMode = settings?.merged.ui?.useTerminalBuffer ?? false;
  const vpGateOpen = isVpMode || bypassVpGate;
  const handlerRef = (0, import_react3.useRef)(handler);
  handlerRef.current = handler;
  const enabled = isActive && isRawModeSupported && vpGateOpen;
  (0, import_react3.useEffect)(() => {
    if (!enabled) return;
    acquireMouseMode(stdout, tracking);
    return () => {
      releaseMouseMode(stdout, tracking);
    };
  }, [enabled, stdout, tracking]);
  const mouseCallback = (0, import_react3.useCallback)((event) => {
    handlerRef.current(event);
  }, []);
  (0, import_react3.useEffect)(() => {
    if (!enabled) return;
    subscribeMouse(mouseCallback);
    return () => unsubscribeMouse(mouseCallback);
  }, [enabled, subscribeMouse, unsubscribeMouse, mouseCallback]);
}
__name(useMouseEvents, "useMouseEvents");

// packages/cli/src/ui/colors.ts
init_esbuild_shims();
var Colors = {
  get type() {
    return themeManager.getActiveTheme().colors.type;
  },
  get Foreground() {
    return themeManager.getActiveTheme().colors.Foreground;
  },
  get Background() {
    return themeManager.getActiveTheme().colors.Background;
  },
  get LightBlue() {
    return themeManager.getActiveTheme().colors.LightBlue;
  },
  get AccentBlue() {
    return themeManager.getActiveTheme().colors.AccentBlue;
  },
  get AccentPurple() {
    return themeManager.getActiveTheme().colors.AccentPurple;
  },
  get AccentCyan() {
    return themeManager.getActiveTheme().colors.AccentCyan;
  },
  get AccentGreen() {
    return themeManager.getActiveTheme().colors.AccentGreen;
  },
  get AccentYellow() {
    return themeManager.getActiveTheme().colors.AccentYellow;
  },
  get AccentRed() {
    return themeManager.getActiveTheme().colors.AccentRed;
  },
  get AccentYellowDim() {
    return themeManager.getActiveTheme().colors.AccentYellowDim;
  },
  get AccentRedDim() {
    return themeManager.getActiveTheme().colors.AccentRedDim;
  },
  get DiffAdded() {
    return themeManager.getActiveTheme().colors.DiffAdded;
  },
  get DiffRemoved() {
    return themeManager.getActiveTheme().colors.DiffRemoved;
  },
  get Comment() {
    return themeManager.getActiveTheme().colors.Comment;
  },
  get Gray() {
    return themeManager.getActiveTheme().colors.Gray;
  },
  get GradientColors() {
    return themeManager.getActiveTheme().colors.GradientColors;
  }
};

// packages/cli/src/ui/utils/measure-element-position.ts
init_esbuild_shims();

// packages/cli/src/ui/utils/list-mouse.ts
init_esbuild_shims();
function frameAnchor(terminalHeight, frameHeight) {
  return Math.min(0, terminalHeight - frameHeight);
}
__name(frameAnchor, "frameAnchor");
function terminalRowToLayoutRow(terminalRow1Based, anchor) {
  return terminalRow1Based - 1 - anchor;
}
__name(terminalRowToLayoutRow, "terminalRowToLayoutRow");
function findItemAtLayoutRow(rects, layoutRow) {
  for (const rect of rects) {
    if (layoutRow >= rect.top && layoutRow < rect.top + rect.height) {
      return rect.index;
    }
  }
  return null;
}
__name(findItemAtLayoutRow, "findItemAtLayoutRow");

// packages/cli/src/ui/utils/measure-element-position.ts
function measureElementPosition(node) {
  const { yogaNode } = node;
  if (!yogaNode) {
    return { x: 0, y: 0, width: 0, height: 0 };
  }
  let x = yogaNode.getComputedLeft();
  let y = yogaNode.getComputedTop();
  let current = node.parentNode;
  while (current) {
    if (current.yogaNode) {
      x += current.yogaNode.getComputedLeft();
      y += current.yogaNode.getComputedTop();
    }
    current = current.parentNode;
  }
  return {
    x,
    y,
    width: yogaNode.getComputedWidth(),
    height: yogaNode.getComputedHeight()
  };
}
__name(measureElementPosition, "measureElementPosition");
function measureFrameHeight(node) {
  let root = node;
  let current = node;
  while (current) {
    root = current;
    current = current.parentNode;
  }
  return root.yogaNode?.getComputedHeight() ?? 0;
}
__name(measureFrameHeight, "measureFrameHeight");
function layoutRowForEvent(node, terminalRow1Based, terminalHeight) {
  const anchor = frameAnchor(terminalHeight, measureFrameHeight(node));
  return terminalRowToLayoutRow(terminalRow1Based, anchor);
}
__name(layoutRowForEvent, "layoutRowForEvent");

// packages/cli/src/ui/components/shared/RowMouseController.tsx
init_esbuild_shims();
var import_react4 = __toESM(require_react(), 1);
function RowMouseController({
  containerRef,
  itemRefs,
  scrollOffset,
  isDisabled,
  onHoverIndex,
  onSelectIndex
}) {
  const { rows: terminalHeight } = useTerminalSize();
  const resolveIndex = (0, import_react4.useCallback)(
    (event) => {
      const container = containerRef.current;
      if (!container) return null;
      const containerRect = measureElementPosition(container);
      const col0 = event.col - 1;
      if (containerRect.width > 0 && (col0 < containerRect.x || col0 >= containerRect.x + containerRect.width)) {
        return null;
      }
      const layoutRow = layoutRowForEvent(container, event.row, terminalHeight);
      const rects = [];
      const nodes = itemRefs.current;
      for (let visiblePos = 0; visiblePos < nodes.length; visiblePos++) {
        const node = nodes[visiblePos];
        if (!node) continue;
        const rect = measureElementPosition(node);
        if (rect.height <= 0) continue;
        rects.push({
          index: scrollOffset + visiblePos,
          top: rect.y,
          height: rect.height
        });
      }
      return findItemAtLayoutRow(rects, layoutRow);
    },
    [containerRef, itemRefs, scrollOffset, terminalHeight]
  );
  const handleMouse = (0, import_react4.useCallback)(
    (event) => {
      if (event.name !== "move" && event.name !== "left-press") return;
      const index = resolveIndex(event);
      if (index === null || isDisabled?.(index)) return;
      if (event.name === "move") {
        onHoverIndex(index);
      } else {
        onSelectIndex(index);
      }
    },
    [resolveIndex, isDisabled, onHoverIndex, onSelectIndex]
  );
  useMouseEvents(handleMouse, { isActive: true, tracking: "any" });
  return null;
}
__name(RowMouseController, "RowMouseController");

export {
  SettingsContext,
  useSettings,
  useTerminalSize,
  Colors,
  useMouseEvents,
  measureElementPosition,
  layoutRowForEvent,
  RowMouseController
};
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 *
 * Inspired by gemini-cli's MouseContext (Google LLC, Apache-2.0): enable SGR
 * mouse mode while at least one subscriber is active, parse mouse sequences
 * out of the KeypressContext pipeline, call each handler, restore on cleanup.
 */
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 *
 * Pure geometry helper for mapping a terminal mouse row onto a vertical list of
 * items. The DOM measurement (reading yoga layout via measureElementPosition)
 * lives in the component that owns the refs; this is pure arithmetic so it can
 * be unit-tested without a renderer.
 */
/**
 * @license
 * Copyright 2025 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 *
 * Shim for ink PR #968 (vadimdemedes/ink#968): extends measureElement()
 * to return {x, y, width, height}. Once the upstream PR is merged and
 * ink is upgraded, replace usages with `measureElement` from 'ink'
 * and delete this file.
 */
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
