#!/usr/bin/env node
import {
  SCROLL_VIEWPORT
} from "./chunk-6OWHQ7HM.js";
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/components/hooks/use-section-scroll.ts
init_esm_shims();
import { useCallback, useEffect, useRef, useState } from "react";
import { measureElement } from "ink";
function useSectionScroll({
  sectionCount,
  focusedIndex,
  availableHeight
}) {
  const sectionRefs = useRef([]);
  const [sectionHeights, setSectionHeights] = useState([]);
  const [scrollTopPx, setScrollTopPx] = useState(0);
  const setSectionRef = useCallback((index, el) => {
    sectionRefs.current[index] = el;
  }, []);
  useEffect(() => {
    const heights = sectionRefs.current.map((el) => {
      if (el) {
        const { height } = measureElement(el);
        return height;
      }
      return 0;
    });
    setSectionHeights((prev) => {
      if (prev.length === heights.length && prev.every((h, i) => h === heights[i])) {
        return prev;
      }
      return heights;
    });
  });
  const scrollEnabled = availableHeight > 0 && availableHeight >= SCROLL_VIEWPORT.MIN_VIEWPORT_ROWS;
  useEffect(() => {
    if (!scrollEnabled || sectionHeights.length === 0) return;
    let topOfFocused = 0;
    for (let i = 0; i < focusedIndex; i++) {
      topOfFocused += sectionHeights[i] ?? 0;
    }
    const focusedHeight = sectionHeights[focusedIndex] ?? 0;
    const bottomOfFocused = topOfFocused + focusedHeight;
    setScrollTopPx((prev) => {
      if (topOfFocused < prev) {
        return topOfFocused;
      }
      if (bottomOfFocused > prev + availableHeight) {
        return bottomOfFocused - availableHeight;
      }
      return prev;
    });
  }, [focusedIndex, sectionHeights, scrollEnabled, availableHeight]);
  return { setSectionRef, scrollEnabled, scrollTopPx };
}
function computeRowScrollTop(focusedRow, currentScrollTop, viewportHeight) {
  if (focusedRow < currentScrollTop) {
    return focusedRow;
  }
  if (focusedRow + 1 > currentScrollTop + viewportHeight) {
    return focusedRow + 1 - viewportHeight;
  }
  return currentScrollTop;
}

export {
  useSectionScroll,
  computeRowScrollTop
};
//# sourceMappingURL=chunk-BNQ5O6LE.js.map