import * as c from "react";
import { useLayoutEffect as j, useEffect as q } from "react";
import { getUserAgent as y } from "./floating-ui.react.utils.js";
import { evaluate as K, max as O, round as z, min as G } from "../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.js";
import { isElement as b } from "../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { getOverflowAncestors as Ie } from "../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import * as D from "react-dom";
import { useFloating as J, offset as Q } 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 { flip as Te, hide as Oe, limitShift as Fe, shift as Me, size as De } 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 { detectOverflow as F } from "../../../../../@floating-ui_dom@1.7.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.js";
import { autoUpdate as Ae, computePosition as Pe, platform as Ne } from "../../../../../@floating-ui_dom@1.7.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.js";
const Y = {
  ...c
}, X = Y.useInsertionEffect, Z = X || ((e) => e());
function W(e) {
  const t = c.useRef(() => {
    if (process.env.NODE_ENV !== "production")
      throw new Error("Cannot call an event handler while rendering.");
  });
  return Z(() => {
    t.current = e;
  }), c.useCallback(function() {
    for (var s = arguments.length, n = new Array(s), o = 0; o < s; o++)
      n[o] = arguments[o];
    return t.current == null ? void 0 : t.current(...n);
  }, []);
}
var H = typeof document < "u" ? j : q;
let P = !1, _ = 0;
const N = () => (
  // Ensure the id is unique with multiple independent versions of Floating UI
  // on <React 18
  "floating-ui-" + Math.random().toString(36).slice(2, 6) + _++
);
function L() {
  const [e, t] = c.useState(() => P ? N() : void 0);
  return H(() => {
    e == null && t(N());
  }, []), c.useEffect(() => {
    P = !0;
  }, []), e;
}
const k = Y.useId, ee = k || L;
let I;
process.env.NODE_ENV !== "production" && (I = /* @__PURE__ */ new Set());
function ne() {
  for (var e, t = arguments.length, s = new Array(t), n = 0; n < t; n++)
    s[n] = arguments[n];
  const o = "Floating UI: " + s.join(" ");
  if (!((e = I) != null && e.has(o))) {
    var f;
    (f = I) == null || f.add(o), console.warn(o);
  }
}
function te() {
  for (var e, t = arguments.length, s = new Array(t), n = 0; n < t; n++)
    s[n] = arguments[n];
  const o = "Floating UI: " + s.join(" ");
  if (!((e = I) != null && e.has(o))) {
    var f;
    (f = I) == null || f.add(o), console.error(o);
  }
}
function oe() {
  const e = /* @__PURE__ */ new Map();
  return {
    emit(t, s) {
      var n;
      (n = e.get(t)) == null || n.forEach((o) => o(s));
    },
    on(t, s) {
      e.set(t, [...e.get(t) || [], s]);
    },
    off(t, s) {
      var n;
      e.set(t, ((n = e.get(t)) == null ? void 0 : n.filter((o) => o !== s)) || []);
    }
  };
}
const re = /* @__PURE__ */ c.createContext(null), le = /* @__PURE__ */ c.createContext(null), ce = () => {
  var e;
  return ((e = c.useContext(re)) == null ? void 0 : e.id) || null;
}, se = () => c.useContext(le), ie = "data-floating-ui-focusable";
function fe(e) {
  const {
    open: t = !1,
    onOpenChange: s,
    elements: n
  } = e, o = ee(), f = c.useRef({}), [u] = c.useState(() => oe()), i = ce() != null;
  if (process.env.NODE_ENV !== "production") {
    const r = n.reference;
    r && !b(r) && te("Cannot pass a virtual element to the `elements.reference` option,", "as it must be a real DOM element. Use `refs.setPositionReference()`", "instead.");
  }
  const [v, d] = c.useState(n.reference), a = W((r, l, g) => {
    f.current.openEvent = r ? l : void 0, u.emit("openchange", {
      open: r,
      event: l,
      reason: g,
      nested: i
    }), s == null || s(r, l, g);
  }), R = c.useMemo(() => ({
    setPositionReference: d
  }), []), p = c.useMemo(() => ({
    reference: v || n.reference || null,
    floating: n.floating || null,
    domReference: n.reference
  }), [v, n.reference, n.floating]);
  return c.useMemo(() => ({
    dataRef: f,
    open: t,
    onOpenChange: a,
    elements: p,
    events: u,
    floatingId: o,
    refs: R
  }), [t, a, p, u, o, R]);
}
function Re(e) {
  e === void 0 && (e = {});
  const {
    nodeId: t
  } = e, s = fe({
    ...e,
    elements: {
      reference: null,
      floating: null,
      ...e.elements
    }
  }), n = e.rootContext || s, o = n.elements, [f, u] = c.useState(null), [i, v] = c.useState(null), a = (o == null ? void 0 : o.domReference) || f, R = c.useRef(null), p = se();
  H(() => {
    a && (R.current = a);
  }, [a]);
  const r = J({
    ...e,
    elements: {
      ...o,
      ...i && {
        reference: i
      }
    }
  }), l = c.useCallback((m) => {
    const E = b(m) ? {
      getBoundingClientRect: () => m.getBoundingClientRect(),
      contextElement: m
    } : m;
    v(E), r.refs.setReference(E);
  }, [r.refs]), g = c.useCallback((m) => {
    (b(m) || m === null) && (R.current = m, u(m)), (b(r.refs.reference.current) || r.refs.reference.current === null || // Don't allow setting virtual elements using the old technique back to
    // `null` to support `positionReference` + an unstable `reference`
    // callback ref.
    m !== null && !b(m)) && r.refs.setReference(m);
  }, [r.refs]), h = c.useMemo(() => ({
    ...r.refs,
    setReference: g,
    setPositionReference: l,
    domReference: R
  }), [r.refs, g, l]), C = c.useMemo(() => ({
    ...r.elements,
    domReference: a
  }), [r.elements, a]), x = c.useMemo(() => ({
    ...r,
    ...n,
    refs: h,
    elements: C,
    nodeId: t
  }), [r, h, C, t, n]);
  return H(() => {
    n.dataRef.current.floatingContext = x;
    const m = p == null ? void 0 : p.nodesRef.current.find((E) => E.id === t);
    m && (m.context = x);
  }), c.useMemo(() => ({
    ...r,
    context: x,
    refs: h,
    elements: C
  }), [r, h, C, x]);
}
const U = "active", B = "selected";
function M(e, t, s) {
  const n = /* @__PURE__ */ new Map(), o = s === "item";
  let f = e;
  if (o && e) {
    const {
      [U]: u,
      [B]: i,
      ...v
    } = e;
    f = v;
  }
  return {
    ...s === "floating" && {
      tabIndex: -1,
      [ie]: ""
    },
    ...f,
    ...t.map((u) => {
      const i = u ? u[s] : null;
      return typeof i == "function" ? e ? i(e) : null : i;
    }).concat(e).reduce((u, i) => (i && Object.entries(i).forEach((v) => {
      let [d, a] = v;
      if (!(o && [U, B].includes(d)))
        if (d.indexOf("on") === 0) {
          if (n.has(d) || n.set(d, []), typeof a == "function") {
            var R;
            (R = n.get(d)) == null || R.push(a), u[d] = function() {
              for (var p, r = arguments.length, l = new Array(r), g = 0; g < r; g++)
                l[g] = arguments[g];
              return (p = n.get(d)) == null ? void 0 : p.map((h) => h(...l)).find((h) => h !== void 0);
            };
          }
        } else
          u[d] = a;
    }), u), {})
  };
}
function Ee(e) {
  e === void 0 && (e = []);
  const t = e.map((i) => i == null ? void 0 : i.reference), s = e.map((i) => i == null ? void 0 : i.floating), n = e.map((i) => i == null ? void 0 : i.item), o = c.useCallback(
    (i) => M(i, e, "reference"),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    t
  ), f = c.useCallback(
    (i) => M(i, e, "floating"),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    s
  ), u = c.useCallback(
    (i) => M(i, e, "item"),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    n
  );
  return c.useMemo(() => ({
    getReferenceProps: o,
    getFloatingProps: f,
    getItemProps: u
  }), [o, f, u]);
}
function V(e, t) {
  return {
    ...e,
    rects: {
      ...e.rects,
      floating: {
        ...e.rects.floating,
        height: t
      }
    }
  };
}
const Ce = (e) => ({
  name: "inner",
  options: e,
  async fn(t) {
    const {
      listRef: s,
      overflowRef: n,
      onFallbackChange: o,
      offset: f = 0,
      index: u = 0,
      minItemsVisible: i = 4,
      referenceOverflowThreshold: v = 0,
      scrollRef: d,
      ...a
    } = K(e, t), {
      rects: R,
      elements: {
        floating: p
      }
    } = t, r = s.current[u], l = (d == null ? void 0 : d.current) || p, g = p.clientTop || l.clientTop, h = p.clientTop !== 0, C = l.clientTop !== 0, x = p === l;
    if (process.env.NODE_ENV !== "production" && (t.placement.startsWith("bottom") || ne('`placement` side must be "bottom" when using the `inner`', "middleware.")), !r)
      return {};
    const m = {
      ...t,
      ...await Q(-r.offsetTop - p.clientTop - R.reference.height / 2 - r.offsetHeight / 2 - f).fn(t)
    }, E = await F(V(m, l.scrollHeight + g + p.clientTop), a), S = await F(m, {
      ...a,
      elementContext: "reference"
    }), w = O(0, E.top), A = m.y + w, $ = (l.scrollHeight > l.clientHeight ? (T) => T : z)(O(0, l.scrollHeight + (h && x || C ? g * 2 : 0) - w - O(0, E.bottom)));
    if (l.style.maxHeight = $ + "px", l.scrollTop = w, o) {
      const T = l.offsetHeight < r.offsetHeight * G(i, s.current.length) - 1 || S.top >= -v || S.bottom >= -v;
      D.flushSync(() => o(T));
    }
    return n && (n.current = await F(V({
      ...m,
      y: A
    }, l.offsetHeight + g + p.clientTop), a)), {
      y: A
    };
  }
});
function xe(e, t) {
  const {
    open: s,
    elements: n
  } = e, {
    enabled: o = !0,
    overflowRef: f,
    scrollRef: u,
    onChange: i
  } = t, v = W(i), d = c.useRef(!1), a = c.useRef(null), R = c.useRef(null);
  c.useEffect(() => {
    if (!o) return;
    function r(g) {
      if (g.ctrlKey || !l || f.current == null)
        return;
      const h = g.deltaY, C = f.current.top >= -0.5, x = f.current.bottom >= -0.5, m = l.scrollHeight - l.clientHeight, E = h < 0 ? -1 : 1, S = h < 0 ? "max" : "min";
      l.scrollHeight <= l.clientHeight || (!C && h > 0 || !x && h < 0 ? (g.preventDefault(), D.flushSync(() => {
        v((w) => w + Math[S](h, m * E));
      })) : /firefox/i.test(y()) && (l.scrollTop += h));
    }
    const l = (u == null ? void 0 : u.current) || n.floating;
    if (s && l)
      return l.addEventListener("wheel", r), requestAnimationFrame(() => {
        a.current = l.scrollTop, f.current != null && (R.current = {
          ...f.current
        });
      }), () => {
        a.current = null, R.current = null, l.removeEventListener("wheel", r);
      };
  }, [o, s, n.floating, f, u, v]);
  const p = c.useMemo(() => ({
    onKeyDown() {
      d.current = !0;
    },
    onWheel() {
      d.current = !1;
    },
    onPointerMove() {
      d.current = !1;
    },
    onScroll() {
      const r = (u == null ? void 0 : u.current) || n.floating;
      if (!(!f.current || !r || !d.current)) {
        if (a.current !== null) {
          const l = r.scrollTop - a.current;
          (f.current.bottom < -0.5 && l < -1 || f.current.top < -0.5 && l > 1) && D.flushSync(() => v((g) => g + l));
        }
        requestAnimationFrame(() => {
          a.current = r.scrollTop;
        });
      }
    }
  }), [n.floating, v, f, u]);
  return c.useMemo(() => o ? {
    floating: p
  } : {}, [o, p]);
}
export {
  Ae as autoUpdate,
  Pe as computePosition,
  F as detectOverflow,
  Te as flip,
  Ie as getOverflowAncestors,
  Oe as hide,
  Ce as inner,
  Fe as limitShift,
  Q as offset,
  Ne as platform,
  Me as shift,
  De as size,
  Re as useFloating,
  ce as useFloatingParentNodeId,
  fe as useFloatingRootContext,
  se as useFloatingTree,
  ee as useId,
  xe as useInnerOffset,
  Ee as useInteractions
};
