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