"use strict";
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/Popover/useFloatingPosition.ts
var useFloatingPosition_exports = {};
__export(useFloatingPosition_exports, {
  useFloatingPosition: () => useFloatingPosition
});
module.exports = __toCommonJS(useFloatingPosition_exports);
var import_react = require("react");
var ABSOLUTE_FLOATING_STYLES = {
  position: "absolute",
  top: 0,
  left: 0,
  width: "max-content"
};
var FIXED_FLOATING_STYLES = {
  position: "fixed",
  top: 0,
  left: 0,
  width: "max-content"
};
var INITIAL_ARROW_STYLES = {
  position: "absolute"
};
var VIEWPORT_PADDING = 0;
var createsFixedContainingBlock = (el) => {
  const style = window.getComputedStyle(el);
  return style.transform !== "none" || style.filter !== "none" || style.backdropFilter !== "none" || style.perspective !== "none" || /\b(transform|filter|perspective)\b/.test(style.willChange);
};
var useFloatingPosition = ({
  referenceRef,
  floatingRef,
  arrowRef,
  isOpen,
  offsetPx,
  arrowWidth,
  useFixedStrategy
}) => {
  const update = (0, import_react.useCallback)(() => {
    const reference = referenceRef.current;
    const floating = floatingRef.current;
    if (!reference || !floating) return;
    const refRect = reference.getBoundingClientRect();
    const floatingWidth = floating.offsetWidth;
    const floatingHeight = floating.offsetHeight;
    const scrollX = useFixedStrategy ? 0 : window.scrollX;
    const scrollY = useFixedStrategy ? 0 : window.scrollY;
    const parent = floating.parentElement;
    const parentIsCb = useFixedStrategy && !!parent && createsFixedContainingBlock(parent);
    const boundsRect = parentIsCb ? parent.getBoundingClientRect() : {
      left: 0,
      top: 0,
      right: window.innerWidth,
      bottom: window.innerHeight
    };
    const bottomTopViewport = refRect.bottom + offsetPx;
    const topTopViewport = refRect.top - floatingHeight - offsetPx;
    const overflowsBottom = bottomTopViewport + floatingHeight > boundsRect.bottom;
    const fitsTop = topTopViewport >= boundsRect.top;
    const nextPlacement = overflowsBottom && fitsTop ? "top" : "bottom";
    const topViewport = nextPlacement === "bottom" ? bottomTopViewport : topTopViewport;
    const rawLeftViewport = refRect.left + refRect.width / 2 - floatingWidth / 2;
    const minLeftViewport = boundsRect.left + VIEWPORT_PADDING;
    const maxLeftViewport = Math.max(
      minLeftViewport,
      boundsRect.right - floatingWidth - VIEWPORT_PADDING
    );
    const leftViewport = Math.min(
      Math.max(minLeftViewport, rawLeftViewport),
      maxLeftViewport
    );
    const cbOffsetLeft = parentIsCb ? boundsRect.left : 0;
    const cbOffsetTop = parentIsCb ? boundsRect.top : 0;
    floating.style.top = `${topViewport + scrollY - cbOffsetTop}px`;
    floating.style.left = `${leftViewport + scrollX - cbOffsetLeft}px`;
    const arrow = arrowRef.current;
    if (!arrow) return;
    const arrowHalf = arrowWidth / 2;
    const refCenterX = refRect.left + refRect.width / 2;
    const rawArrowLeft = refCenterX - leftViewport - arrowHalf;
    const maxArrowLeft = Math.max(0, floatingWidth - arrowWidth);
    const arrowLeft = Math.min(Math.max(0, rawArrowLeft), maxArrowLeft);
    arrow.style.left = `${arrowLeft}px`;
    if (nextPlacement === "bottom") {
      arrow.style.bottom = "100%";
      arrow.style.top = "";
      arrow.style.transform = "rotate(180deg)";
    } else {
      arrow.style.top = "100%";
      arrow.style.bottom = "";
      arrow.style.transform = "";
    }
  }, [
    referenceRef,
    floatingRef,
    arrowRef,
    offsetPx,
    arrowWidth,
    useFixedStrategy
  ]);
  (0, import_react.useLayoutEffect)(() => {
    if (!isOpen) return;
    update();
  }, [isOpen, update]);
  return {
    initialFloatingStyles: useFixedStrategy ? FIXED_FLOATING_STYLES : ABSOLUTE_FLOATING_STYLES,
    initialArrowStyles: INITIAL_ARROW_STYLES,
    update
  };
};
//# sourceMappingURL=useFloatingPosition.js.map
