import * as a from "react";
import { getSide as H, getSideAxis as ke, getAlignment as He } from "../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.js";
import { ownerDocument as Qe } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/owner.js";
import { useIsoLayoutEffect as G } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useIsoLayoutEffect.js";
import { useValueAsRef as J } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useValueAsRef.js";
import { useStableCallback as Ze } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useStableCallback.js";
import { useDirection as et } from "../direction-provider/DirectionContext.js";
import { arrow as tt } from "../floating-ui-react/middleware/arrow.js";
import { hide as nt } from "./hideMiddleware.js";
import { DEFAULT_SIDES as ot } from "./adaptiveOriginMiddleware.js";
import { flip as it, offset as rt, size as st, shift as ct, limitShift as lt } from "../../../../../../@floating-ui_react-dom@2.1.6_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@floating-ui/react-dom/dist/floating-ui.react-dom.js";
import { useFloating as at } from "../floating-ui-react/hooks/useFloating.js";
import { autoUpdate as Ce } from "../../../../../../@floating-ui_dom@1.7.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.js";
function Te(l, f, u) {
  const n = l === "inline-start" || l === "inline-end";
  return {
    top: "top",
    right: n ? u ? "inline-start" : "inline-end" : "right",
    bottom: "bottom",
    left: n ? u ? "inline-end" : "inline-start" : "left"
  }[f];
}
function Fe(l, f, u) {
  const {
    rects: n,
    placement: s
  } = l;
  return {
    side: Te(f, H(s), u),
    align: He(s) || "center",
    anchor: {
      width: n.reference.width,
      height: n.reference.height
    },
    positioner: {
      width: n.floating.width,
      height: n.floating.height
    }
  };
}
function Pt(l) {
  var Pe, $e;
  const {
    // Public parameters
    anchor: f,
    positionMethod: u = "absolute",
    side: n = "bottom",
    sideOffset: s = 0,
    align: A = "center",
    alignOffset: T = 0,
    collisionBoundary: K,
    collisionPadding: ze = 5,
    sticky: z = !1,
    arrowPadding: N = 5,
    disableAnchorTracking: B = !1,
    // Private parameters
    keepMounted: V = !1,
    floatingRootContext: D,
    mounted: r,
    collisionAvoidance: I,
    shiftCrossAxis: w = !1,
    nodeId: Be,
    adaptiveOrigin: U,
    lazyFlip: Q = !1,
    externalTree: Ve
  } = l, [Z, ee] = a.useState(null);
  !r && Z !== null && ee(null);
  const S = I.side || "flip", R = I.align || "flip", Ie = I.fallbackAxisSide || "end", te = typeof f == "function" ? f : void 0, Ue = Ze(te), ne = te ? Ue : f, E = J(f), p = et() === "rtl", oe = Z || {
    top: "top",
    right: "right",
    bottom: "bottom",
    left: "left",
    "inline-end": p ? "left" : "right",
    "inline-start": p ? "right" : "left"
  }[n], Ye = A === "center" ? oe : `${oe}-${A}`;
  let t = ze;
  const d = 1, ie = n === "bottom" ? d : 0, re = n === "top" ? d : 0, se = n === "right" ? d : 0, ce = n === "left" ? d : 0;
  typeof t == "number" ? t = {
    top: t + ie,
    right: t + ce,
    bottom: t + re,
    left: t + se
  } : t && (t = {
    top: (t.top || 0) + ie,
    right: (t.right || 0) + ce,
    bottom: (t.bottom || 0) + re,
    left: (t.left || 0) + se
  });
  const M = {
    boundary: K === "clipping-ancestors" ? "clippingAncestors" : K,
    padding: t
  }, m = a.useRef(null), Y = J(s), W = J(T), k = [rt((e) => {
    const o = Fe(e, n, p), c = typeof Y.current == "function" ? Y.current(o) : Y.current, i = typeof W.current == "function" ? W.current(o) : W.current;
    return {
      mainAxis: c,
      crossAxis: i,
      alignmentAxis: i
    };
  }, [typeof s != "function" ? s : 0, typeof T != "function" ? T : 0, p, n])], le = R === "none" && S !== "shift", ae = !le && (z || w || S === "shift"), fe = S === "none" ? null : it({
    ...M,
    // Ensure the popup flips if it's been limited by its --available-height and it resizes.
    // Since the size() padding is smaller than the flip() padding, flip() will take precedence.
    padding: {
      top: t.top + d,
      right: t.right + d,
      bottom: t.bottom + d,
      left: t.left + d
    },
    mainAxis: !w && S === "flip",
    crossAxis: R === "flip" ? "alignment" : !1,
    fallbackAxisSideDirection: Ie
  }), de = le ? null : ct((e) => {
    const o = Qe(e.elements.floating).documentElement;
    return {
      ...M,
      // Use the Layout Viewport to avoid shifting around when pinch-zooming
      // for context menus.
      rootBoundary: w ? {
        x: 0,
        y: 0,
        width: o.clientWidth,
        height: o.clientHeight
      } : void 0,
      mainAxis: R !== "none",
      crossAxis: ae,
      limiter: z || w ? void 0 : lt((c) => {
        if (!m.current)
          return {};
        const {
          width: i,
          height: x
        } = m.current.getBoundingClientRect(), y = ke(H(c.placement)), L = y === "y" ? i : x, _ = y === "y" ? t.left + t.right : t.top + t.bottom;
        return {
          offset: L / 2 + _ / 2
        };
      })
    };
  }, [M, z, w, t, R]);
  S === "shift" || R === "shift" || A === "center" ? k.push(de, fe) : k.push(fe, de), k.push(st({
    ...M,
    apply({
      elements: {
        floating: e
      },
      rects: {
        reference: o
      },
      availableWidth: c,
      availableHeight: i
    }) {
      Object.entries({
        "--available-width": `${c}px`,
        "--available-height": `${i}px`,
        "--anchor-width": `${o.width}px`,
        "--anchor-height": `${o.height}px`
      }).forEach(([x, y]) => {
        e.style.setProperty(x, y);
      });
    }
  }), tt(() => ({
    // `transform-origin` calculations rely on an element existing. If the arrow hasn't been set,
    // we'll create a fake element.
    element: m.current || document.createElement("div"),
    padding: N,
    offsetParent: "floating"
  }), [N]), {
    name: "transformOrigin",
    fn(e) {
      var De, Ee, Me;
      const {
        elements: o,
        middlewareData: c,
        placement: i,
        rects: x,
        y
      } = e, L = H(i), _ = ke(L), b = m.current, We = ((De = c.arrow) == null ? void 0 : De.x) || 0, Xe = ((Ee = c.arrow) == null ? void 0 : Ee.y) || 0, je = (b == null ? void 0 : b.clientWidth) || 0, _e = (b == null ? void 0 : b.clientHeight) || 0, q = We + je / 2, ve = Xe + _e / 2, qe = Math.abs(((Me = c.shift) == null ? void 0 : Me.y) || 0), Ge = x.reference.height / 2, v = typeof s == "function" ? s(Fe(e, n, p)) : s, Je = qe > v, Ke = {
        top: `${q}px calc(100% + ${v}px)`,
        bottom: `${q}px ${-v}px`,
        left: `calc(100% + ${v}px) ${ve}px`,
        right: `${-v}px ${ve}px`
      }[L], Ne = `${q}px ${x.reference.y + Ge - y}px`;
      return o.floating.style.setProperty("--transform-origin", ae && _ === "y" && Je ? Ne : Ke), {};
    }
  }, nt, U), G(() => {
    !r && D && D.update({
      referenceElement: null,
      floatingElement: null,
      domReferenceElement: null
    });
  }, [r, D]);
  const X = a.useMemo(() => ({
    elementResize: !B && typeof ResizeObserver < "u",
    layoutShift: !B && typeof IntersectionObserver < "u"
  }), [B]), {
    refs: h,
    elements: O,
    x: ue,
    y: pe,
    middlewareData: g,
    update: C,
    placement: me,
    context: he,
    isPositioned: P,
    floatingStyles: ge
  } = at({
    rootContext: D,
    placement: Ye,
    middleware: k,
    strategy: u,
    whileElementsMounted: V ? void 0 : (...e) => Ce(...e, X),
    nodeId: Be,
    externalTree: Ve
  }), {
    sideX: xe,
    sideY: ye
  } = g.adaptiveOrigin || ot, j = P ? u : "fixed", be = a.useMemo(() => U ? {
    position: j,
    [xe]: ue,
    [ye]: pe
  } : {
    position: j,
    ...ge
  }, [U, j, xe, ue, ye, pe, ge]), F = a.useRef(null);
  G(() => {
    if (!r)
      return;
    const e = E.current, o = typeof e == "function" ? e() : e, i = (Le(o) ? o.current : o) || null || null;
    i !== F.current && (h.setPositionReference(i), F.current = i);
  }, [r, h, ne, E]), a.useEffect(() => {
    if (!r)
      return;
    const e = E.current;
    typeof e != "function" && Le(e) && e.current !== F.current && (h.setPositionReference(e.current), F.current = e.current);
  }, [r, h, ne, E]), a.useEffect(() => {
    if (V && r && O.domReference && O.floating)
      return Ce(O.domReference, O.floating, C, X);
  }, [V, r, O, C, X]);
  const $ = H(me), Ae = Te(n, $, p), we = He(me) || "center", Se = !!((Pe = g.hide) != null && Pe.referenceHidden);
  G(() => {
    Q && r && P && ee($);
  }, [Q, r, P, $]);
  const Re = a.useMemo(() => {
    var e, o;
    return {
      position: "absolute",
      top: (e = g.arrow) == null ? void 0 : e.y,
      left: (o = g.arrow) == null ? void 0 : o.x
    };
  }, [g.arrow]), Oe = (($e = g.arrow) == null ? void 0 : $e.centerOffset) !== 0;
  return a.useMemo(() => ({
    positionerStyles: be,
    arrowStyles: Re,
    arrowRef: m,
    arrowUncentered: Oe,
    side: Ae,
    align: we,
    physicalSide: $,
    anchorHidden: Se,
    refs: h,
    context: he,
    isPositioned: P,
    update: C
  }), [be, Re, m, Oe, Ae, we, $, Se, h, he, P, C]);
}
function Le(l) {
  return l != null && "current" in l;
}
export {
  Pt as useAnchorPositioning
};
