"use client";
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/hooks/useFloatingElement.ts
var useFloatingElement_exports = {};
__export(useFloatingElement_exports, {
  useFloatingElement: () => useFloatingElement
});
module.exports = __toCommonJS(useFloatingElement_exports);
var import_react2 = require("react");

// src/utils/math.ts
var clamp = (value, range = [0, 1]) => {
  const [min, max] = range;
  return Math.min(Math.max(value, min), max);
};

// src/hooks/focus/useIsMounted.ts
var import_react = require("react");
var isClient = typeof window !== "undefined" && typeof document !== "undefined";
var useIsomorphicEffect = isClient ? import_react.useLayoutEffect : import_react.useEffect;
var useIsMounted = () => {
  const [isMounted, setIsMounted] = (0, import_react.useState)(false);
  useIsomorphicEffect(() => {
    setIsMounted(true);
    return () => {
      setIsMounted(false);
    };
  }, []);
  return isMounted;
};

// src/hooks/useFloatingElement.ts
function calculatePosition({
  windowRect,
  containerRect,
  anchorRect,
  options
}) {
  const { verticalAlignment, horizontalAlignment, gap, screenPadding } = options;
  const windowWidth = windowRect.width;
  const windowHeight = windowRect.height;
  const maxWidth = windowWidth - 2 * screenPadding;
  const maxHeight = windowHeight - 2 * screenPadding;
  const width = Math.min(containerRect.width, maxWidth);
  const height = Math.min(containerRect.height, maxHeight);
  const leftSuggestion = {
    beforeStart: anchorRect.left - width - gap,
    afterStart: anchorRect.left,
    center: anchorRect.left + anchorRect.width / 2 - width / 2,
    beforeEnd: anchorRect.right - width,
    afterEnd: anchorRect.right + gap
  }[horizontalAlignment];
  const topSuggestion = {
    beforeStart: anchorRect.top - height - gap,
    afterStart: anchorRect.top,
    center: anchorRect.top + anchorRect.height / 2 - height / 2,
    beforeEnd: anchorRect.bottom - height,
    afterEnd: anchorRect.bottom + gap
  }[verticalAlignment];
  const left = clamp(leftSuggestion, [
    screenPadding,
    windowWidth - screenPadding - width
  ]);
  const top = clamp(topSuggestion, [
    screenPadding,
    windowHeight - screenPadding - height
  ]);
  return {
    left,
    top,
    maxWidth,
    maxHeight
  };
}
function useFloatingElement({
  active = true,
  windowRef,
  anchorRef,
  containerRef,
  isPolling = false,
  pollingInterval = 100,
  verticalAlignment = "afterEnd",
  horizontalAlignment = "afterStart",
  screenPadding = 16,
  gap = 4
}) {
  const [style, setStyle] = (0, import_react2.useState)();
  const isMounted = useIsMounted();
  const calculate = (0, import_react2.useCallback)(() => {
    const containerRect = containerRef.current.getBoundingClientRect();
    const windowRect = windowRef?.current.getBoundingClientRect() ?? {
      top: 0,
      bottom: window.innerHeight,
      left: 0,
      right: window.innerWidth,
      width: window.innerWidth,
      height: window.innerHeight
    };
    const anchorElement = anchorRef?.current;
    if (anchorRef && !anchorElement) {
      console.warn("FloatingContainer anchor provided, but its value is undefined");
    }
    const anchorRect = anchorElement?.getBoundingClientRect() ?? windowRect;
    const calculateProps = {
      windowRect,
      anchorRect,
      containerRect,
      options: {
        horizontalAlignment,
        verticalAlignment,
        screenPadding,
        gap
      }
    };
    setStyle(calculatePosition(calculateProps));
  }, [anchorRef, containerRef, gap, horizontalAlignment, screenPadding, verticalAlignment, windowRef]);
  const height = containerRef.current?.getBoundingClientRect().height;
  const width = containerRef.current?.getBoundingClientRect().width;
  (0, import_react2.useEffect)(() => {
    if (active && isMounted) {
      calculate();
    } else {
      setStyle(void 0);
    }
  }, [calculate, active, isMounted, height, width]);
  (0, import_react2.useEffect)(() => {
    window.addEventListener("resize", calculate);
    let timeout;
    if (isPolling) {
      timeout = setInterval(calculate, pollingInterval);
    }
    return () => {
      window.removeEventListener("resize", calculate);
      if (timeout) {
        clearInterval(timeout);
      }
    };
  }, [calculate, isPolling, pollingInterval]);
  return style;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFloatingElement
});
//# sourceMappingURL=useFloatingElement.cjs.map