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/PisellProcedure/hooks/useSidebarHeight.ts
var useSidebarHeight_exports = {};
__export(useSidebarHeight_exports, {
  useSidebarHeight: () => useSidebarHeight
});
module.exports = __toCommonJS(useSidebarHeight_exports);
var import_react = require("react");
var useSidebarHeight = (isSizeStable, sizeValues) => {
  const [siderHeight, setSiderHeight] = (0, import_react.useState)(void 0);
  const hasInitializedRef = (0, import_react.useRef)(false);
  const rafIdRef = (0, import_react.useRef)(null);
  const stableCheckCountRef = (0, import_react.useRef)(0);
  const previousSiderHeightRef = (0, import_react.useRef)(void 0);
  const sizeValuesRef = (0, import_react.useRef)(sizeValues);
  (0, import_react.useEffect)(() => {
    sizeValuesRef.current = sizeValues;
  }, [sizeValues]);
  const getBodyScaleY = (0, import_react.useCallback)(() => {
    try {
      if (typeof window === "undefined" || typeof document === "undefined") {
        return 1;
      }
      const bodyTransform = getComputedStyle(document.body).transform;
      let scaleY = 1;
      if (bodyTransform !== "none") {
        const matrix = new DOMMatrix(bodyTransform);
        scaleY = matrix.d;
      }
      return scaleY;
    } catch (error) {
      return 1;
    }
  }, []);
  const calculateHeight = (0, import_react.useCallback)(() => {
    const { containerSelector, containerHeight, headerHeight, footerHeight } = sizeValuesRef.current;
    let baseHeight;
    if (containerSelector && containerSelector.trim() && containerHeight && containerHeight > 0) {
      baseHeight = containerHeight;
    } else {
      baseHeight = typeof window !== "undefined" ? window.innerHeight : 0;
    }
    const bodyPadding = 16 * 2;
    const headerHeightValue = headerHeight || 0;
    const calculatedHeight = baseHeight - headerHeightValue - footerHeight - bodyPadding;
    const scaleY = getBodyScaleY();
    const scaledHeight = calculatedHeight / scaleY;
    return Math.round(scaledHeight * 100) / 100;
  }, [sizeValuesRef.current, getBodyScaleY]);
  (0, import_react.useEffect)(() => {
    if (typeof window === "undefined" || typeof document === "undefined") {
      return;
    }
    if (!isSizeStable) {
      hasInitializedRef.current = false;
      stableCheckCountRef.current = 0;
      if (rafIdRef.current !== null) {
        cancelAnimationFrame(rafIdRef.current);
        rafIdRef.current = null;
      }
      return;
    }
    if (rafIdRef.current !== null) {
      cancelAnimationFrame(rafIdRef.current);
    }
    const checkStability = () => {
      if (!isSizeStable) {
        stableCheckCountRef.current = 0;
        return;
      }
      const calculatedHeight = calculateHeight();
      if (calculatedHeight <= 0) {
        stableCheckCountRef.current = 0;
        return;
      }
      const previousHeight = previousSiderHeightRef.current;
      if (hasInitializedRef.current && previousHeight !== void 0 && Math.abs(calculatedHeight - previousHeight) < 10) {
        return;
      }
      stableCheckCountRef.current += 1;
      if (stableCheckCountRef.current >= 3) {
        previousSiderHeightRef.current = calculatedHeight;
        setSiderHeight(calculatedHeight);
        hasInitializedRef.current = true;
        stableCheckCountRef.current = 0;
      } else {
        rafIdRef.current = requestAnimationFrame(checkStability);
      }
    };
    rafIdRef.current = requestAnimationFrame(checkStability);
    return () => {
      if (rafIdRef.current !== null) {
        cancelAnimationFrame(rafIdRef.current);
        rafIdRef.current = null;
      }
    };
  }, [isSizeStable, calculateHeight]);
  return siderHeight;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useSidebarHeight
});
