import { arrow as L, computePosition as H } from "../../dom/dist/floating-ui.dom.js";
import { autoUpdate as b, flip as ee, hide as te, limitShift as re, offset as ne, platform as fe, shift as oe, size as ie } from "../../dom/dist/floating-ui.dom.js";
import * as f from "react";
import { useLayoutEffect as I, useEffect as J } from "react";
import * as K from "react-dom";
const T = (e) => {
  function t(r) {
    return {}.hasOwnProperty.call(r, "current");
  }
  return {
    name: "arrow",
    options: e,
    fn(r) {
      const {
        element: n,
        padding: i
      } = typeof e == "function" ? e(r) : e;
      return n && t(n) ? n.current != null ? L({
        element: n.current,
        padding: i
      }).fn(r) : {} : n ? L({
        element: n,
        padding: i
      }).fn(r) : {};
    }
  };
};
var R = typeof document < "u" ? I : J;
function h(e, t) {
  if (e === t)
    return !0;
  if (typeof e != typeof t)
    return !1;
  if (typeof e == "function" && e.toString() === t.toString())
    return !0;
  let r, n, i;
  if (e && t && typeof e == "object") {
    if (Array.isArray(e)) {
      if (r = e.length, r !== t.length)
        return !1;
      for (n = r; n-- !== 0; )
        if (!h(e[n], t[n]))
          return !1;
      return !0;
    }
    if (i = Object.keys(e), r = i.length, r !== Object.keys(t).length)
      return !1;
    for (n = r; n-- !== 0; )
      if (!{}.hasOwnProperty.call(t, i[n]))
        return !1;
    for (n = r; n-- !== 0; ) {
      const p = i[n];
      if (!(p === "_owner" && e.$$typeof) && !h(e[p], t[p]))
        return !1;
    }
    return !0;
  }
  return e !== e && t !== t;
}
function A(e) {
  return typeof window > "u" ? 1 : (e.ownerDocument.defaultView || window).devicePixelRatio || 1;
}
function $(e, t) {
  const r = A(e);
  return Math.round(t * r) / r;
}
function v(e) {
  const t = f.useRef(e);
  return R(() => {
    t.current = e;
  }), t;
}
function X(e) {
  e === void 0 && (e = {});
  const {
    placement: t = "bottom",
    strategy: r = "absolute",
    middleware: n = [],
    platform: i,
    elements: {
      reference: p,
      floating: q
    } = {},
    transform: E = !0,
    whileElementsMounted: k,
    open: D
  } = e, [u, O] = f.useState({
    x: 0,
    y: 0,
    strategy: r,
    placement: t,
    middlewareData: {},
    isPositioned: !1
  }), [x, z] = f.useState(n);
  h(x, n) || z(n);
  const [B, U] = f.useState(null), [V, W] = f.useState(null), _ = f.useCallback((o) => {
    o !== a.current && (a.current = o, U(o));
  }, []), C = f.useCallback((o) => {
    o !== d.current && (d.current = o, W(o));
  }, []), s = p || B, l = q || V, a = f.useRef(null), d = f.useRef(null), y = f.useRef(u), G = k != null, M = v(k), P = v(i), g = f.useCallback(() => {
    if (!a.current || !d.current)
      return;
    const o = {
      placement: t,
      strategy: r,
      middleware: x
    };
    P.current && (o.platform = P.current), H(a.current, d.current, o).then((w) => {
      const m = {
        ...w,
        isPositioned: !0
      };
      S.current && !h(y.current, m) && (y.current = m, K.flushSync(() => {
        O(m);
      }));
    });
  }, [x, t, r, P]);
  R(() => {
    D === !1 && y.current.isPositioned && (y.current.isPositioned = !1, O((o) => ({
      ...o,
      isPositioned: !1
    })));
  }, [D]);
  const S = f.useRef(!1);
  R(() => (S.current = !0, () => {
    S.current = !1;
  }), []), R(() => {
    if (s && (a.current = s), l && (d.current = l), s && l) {
      if (M.current)
        return M.current(s, l, g);
      g();
    }
  }, [s, l, g, M, G]);
  const F = f.useMemo(() => ({
    reference: a,
    floating: d,
    setReference: _,
    setFloating: C
  }), [_, C]), c = f.useMemo(() => ({
    reference: s,
    floating: l
  }), [s, l]), j = f.useMemo(() => {
    const o = {
      position: r,
      left: 0,
      top: 0
    };
    if (!c.floating)
      return o;
    const w = $(c.floating, u.x), m = $(c.floating, u.y);
    return E ? {
      ...o,
      transform: "translate(" + w + "px, " + m + "px)",
      ...A(c.floating) >= 1.5 && {
        willChange: "transform"
      }
    } : {
      position: r,
      left: w,
      top: m
    };
  }, [r, E, c.floating, u.x, u.y]);
  return f.useMemo(() => ({
    ...u,
    update: g,
    refs: F,
    elements: c,
    floatingStyles: j
  }), [u, g, F, c, j]);
}
export {
  T as arrow,
  b as autoUpdate,
  H as computePosition,
  ee as flip,
  te as hide,
  re as limitShift,
  ne as offset,
  fe as platform,
  oe as shift,
  ie as size,
  X as useFloating
};
