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);

// src/components/PisellScrollView/hooks/useScrollState.ts
var useScrollState_exports = {};
__export(useScrollState_exports, {
  useScrollState: () => useScrollState
});
module.exports = __toCommonJS(useScrollState_exports);
var import_react = require("react");
var debounce = (fn, delay) => {
  let timeoutId = null;
  return (...args) => {
    if (timeoutId) {
      clearTimeout(timeoutId);
    }
    timeoutId = setTimeout(() => {
      fn(...args);
      timeoutId = null;
    }, delay);
  };
};
var isScrollContainerMeasurable = (container) => {
  const { clientWidth, clientHeight } = container;
  if (clientWidth > 0 || clientHeight > 0) {
    return true;
  }
  const { width, height } = container.getBoundingClientRect();
  return width > 0 || height > 0;
};
var SCROLL_STATE_KEYS = [
  "scrollTop",
  "scrollLeft",
  "percentY",
  "percentX",
  "scrollHeight",
  "scrollWidth",
  "clientHeight",
  "clientWidth",
  "direction",
  "isScrollToTop",
  "isScrollToBottom",
  "isScrollToLeft",
  "isScrollToRight",
  "isOverflows"
];
var isScrollStateEqual = (prev, next) => SCROLL_STATE_KEYS.every((key) => prev[key] === next[key]);
var useScrollState = ({
  containerRef,
  overflow
}) => {
  const [scrollState, setScrollState] = (0, import_react.useState)(() => ({
    scrollTop: 0,
    scrollLeft: 0,
    percentY: 0,
    percentX: 0,
    scrollHeight: 0,
    scrollWidth: 0,
    clientHeight: 0,
    clientWidth: 0,
    direction: null,
    isScrollToTop: overflow === "y" || overflow === "both",
    isScrollToBottom: false,
    isScrollToLeft: overflow === "x" || overflow === "both",
    isScrollToRight: false,
    isOverflows: false
  }));
  const isMountedRef = (0, import_react.useRef)(true);
  const resizeObserverRef = (0, import_react.useRef)(null);
  const calculateScrollState = (0, import_react.useCallback)(
    (prevState = null) => {
      const container = containerRef.current;
      if (!container) return null;
      try {
        if (!isScrollContainerMeasurable(container)) {
          return null;
        }
        const {
          scrollTop,
          scrollLeft,
          scrollHeight,
          scrollWidth,
          clientHeight,
          clientWidth
        } = container;
        const isOverflows = scrollHeight > clientHeight || scrollWidth > clientWidth;
        const isScrollToTop = scrollTop <= 0;
        const isScrollToBottom = scrollTop + clientHeight >= scrollHeight - 1;
        const isScrollToLeft = scrollLeft <= 0;
        const isScrollToRight = scrollLeft + clientWidth >= scrollWidth - 1;
        let direction = null;
        if (prevState) {
          const deltaY = Math.abs(scrollTop - prevState.scrollTop);
          const deltaX = Math.abs(scrollLeft - prevState.scrollLeft);
          if (deltaY > deltaX && deltaY > 0) {
            direction = scrollTop > prevState.scrollTop ? "down" : "up";
          } else if (deltaX > 0) {
            direction = scrollLeft > prevState.scrollLeft ? "right" : "left";
          } else {
            direction = prevState.direction;
          }
        }
        const maxScrollTop = Math.max(0, scrollHeight - clientHeight);
        const maxScrollLeft = Math.max(0, scrollWidth - clientWidth);
        const percentY = maxScrollTop > 0 ? scrollTop / maxScrollTop * 100 : 0;
        const percentX = maxScrollLeft > 0 ? scrollLeft / maxScrollLeft * 100 : 0;
        return {
          scrollTop,
          scrollLeft,
          scrollHeight,
          scrollWidth,
          clientHeight,
          clientWidth,
          percentY,
          percentX,
          direction,
          isScrollToTop,
          isScrollToBottom,
          isScrollToLeft,
          isScrollToRight,
          isOverflows
        };
      } catch (error) {
        console.warn("[useScrollState] 计算滚动状态时出错:", error);
        return null;
      }
    },
    [containerRef]
  );
  const updateScrollState = (0, import_react.useCallback)(() => {
    if (!isMountedRef.current) return;
    setScrollState((prev) => {
      const newState = calculateScrollState(prev);
      if (!newState) {
        return prev;
      }
      return isScrollStateEqual(prev, newState) ? prev : newState;
    });
  }, [calculateScrollState]);
  const updateOverflowState = (0, import_react.useCallback)(() => {
    if (!isMountedRef.current) return;
    const newState = calculateScrollState(null);
    if (!newState) {
      return;
    }
    setScrollState((prev) => {
      const next = {
        ...prev,
        scrollHeight: newState.scrollHeight,
        scrollWidth: newState.scrollWidth,
        clientHeight: newState.clientHeight,
        clientWidth: newState.clientWidth,
        isOverflows: newState.isOverflows,
        isScrollToTop: newState.isScrollToTop,
        isScrollToBottom: newState.isScrollToBottom,
        isScrollToLeft: newState.isScrollToLeft,
        isScrollToRight: newState.isScrollToRight
      };
      return isScrollStateEqual(prev, next) ? prev : next;
    });
  }, [calculateScrollState]);
  (0, import_react.useEffect)(() => {
    isMountedRef.current = true;
    const container = containerRef.current;
    if (!container) return;
    const debouncedUpdateOverflow = debounce(updateOverflowState, 50);
    updateOverflowState();
    if (typeof ResizeObserver === "undefined") {
      const handleResize = () => debouncedUpdateOverflow();
      window.addEventListener("resize", handleResize);
      return () => {
        isMountedRef.current = false;
        window.removeEventListener("resize", handleResize);
      };
    }
    try {
      resizeObserverRef.current = new ResizeObserver((entries) => {
        if (entries.length > 0 && isMountedRef.current) {
          debouncedUpdateOverflow();
        }
      });
      resizeObserverRef.current.observe(container);
      const children = container.children;
      for (let i = 0; i < children.length; i++) {
        resizeObserverRef.current.observe(children[i]);
      }
      const mutationObserver = new MutationObserver((mutations) => {
        if (!isMountedRef.current || !resizeObserverRef.current) return;
        mutations.forEach((mutation) => {
          mutation.addedNodes.forEach((node) => {
            var _a;
            if (node instanceof Element) {
              (_a = resizeObserverRef.current) == null ? void 0 : _a.observe(node);
            }
          });
        });
        debouncedUpdateOverflow();
      });
      mutationObserver.observe(container, {
        childList: true,
        subtree: false
      });
      const handleWindowResize = () => debouncedUpdateOverflow();
      window.addEventListener("resize", handleWindowResize);
      return () => {
        var _a;
        isMountedRef.current = false;
        (_a = resizeObserverRef.current) == null ? void 0 : _a.disconnect();
        resizeObserverRef.current = null;
        mutationObserver.disconnect();
        window.removeEventListener("resize", handleWindowResize);
      };
    } catch (error) {
      console.warn("[useScrollState] 初始化 ResizeObserver 失败:", error);
      const handleResize = () => debouncedUpdateOverflow();
      window.addEventListener("resize", handleResize);
      return () => {
        isMountedRef.current = false;
        window.removeEventListener("resize", handleResize);
      };
    }
  }, [containerRef, updateOverflowState]);
  return { scrollState, updateScrollState };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useScrollState
});
