import { useFloating as _, inner as ee, useInnerOffset as te, useInteractions as ne } from "../../../../../../@floating-ui_react@0.26.28_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@floating-ui/react/dist/floating-ui.react.js";
import * as q from "react";
import { useMemo as b, useContext as H, createContext as U, useCallback as le, useState as M, useRef as re } from "react";
import { useDisposables as oe } from "../hooks/use-disposables.js";
import { useEvent as z } from "../hooks/use-event.js";
import { useIsoMorphicEffect as $ } from "../hooks/use-iso-morphic-effect.js";
import { offset as ie, shift as ae, flip as se, size as ue } 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 { autoUpdate as fe } from "../../../../../../@floating-ui_dom@1.7.4/node_modules/@floating-ui/dom/dist/floating-ui.dom.js";
let P = U({ styles: void 0, setReference: () => {
}, setFloating: () => {
}, getReferenceProps: () => ({}), getFloatingProps: () => ({}), slot: {} });
P.displayName = "FloatingContext";
let k = U(null);
k.displayName = "PlacementContext";
function Fe(e) {
  return b(() => e ? typeof e == "string" ? { to: e } : e : null, [e]);
}
function be() {
  return H(P).setReference;
}
function Pe() {
  let { getFloatingProps: e, slot: t } = H(P);
  return le((...n) => Object.assign({}, e(...n), { "data-anchor": t.anchor }), [e, t]);
}
function we(e = null) {
  e === !1 && (e = null), typeof e == "string" && (e = { to: e });
  let t = H(k), n = b(() => e, [JSON.stringify(e, (r, i) => {
    var u;
    return (u = i == null ? void 0 : i.outerHTML) != null ? u : i;
  })]);
  $(() => {
    t == null || t(n ?? null);
  }, [t, n]);
  let l = H(P);
  return b(() => [l.setFloating, e ? l.styles : {}], [l.setFloating, e, l.styles]);
}
let D = 4;
function Ce({ children: e, enabled: t = !0 }) {
  let [n, l] = M(null), [r, i] = M(0), u = re(null), [p, a] = M(null);
  pe(p);
  let o = t && n !== null && p !== null, { to: y = "bottom", gap: E = 0, offset: w = 0, padding: c = 0, inner: d } = ce(n, p), [s, f = "center"] = y.split(" ");
  $(() => {
    o && i(0);
  }, [o]);
  let { refs: x, floatingStyles: N, context: m } = _({ open: o, placement: s === "selection" ? f === "center" ? "bottom" : `bottom-${f}` : f === "center" ? `${s}` : `${s}-${f}`, strategy: "absolute", transform: !1, middleware: [ie({ mainAxis: s === "selection" ? 0 : E, crossAxis: w }), ae({ padding: c }), s !== "selection" && se({ padding: c }), s === "selection" && d ? ee({ ...d, padding: c, overflowRef: u, offset: r, minItemsVisible: D, referenceOverflowThreshold: c, onFallbackChange(h) {
    var C, R;
    if (!h) return;
    let F = m.elements.floating;
    if (!F) return;
    let B = parseFloat(getComputedStyle(F).scrollPaddingBottom) || 0, O = Math.min(D, F.childElementCount), V = 0, j = 0;
    for (let g of (R = (C = m.elements.floating) == null ? void 0 : C.childNodes) != null ? R : []) if (g instanceof HTMLElement) {
      let v = g.offsetTop, L = v + g.clientHeight + B, T = F.scrollTop, W = T + F.clientHeight;
      if (v >= T && L <= W) O--;
      else {
        j = Math.max(0, Math.min(L, W) - Math.max(v, T)), V = g.clientHeight;
        break;
      }
    }
    O >= 1 && i((g) => {
      let v = V * O - j + B;
      return g >= v ? g : v;
    });
  } }) : null, ue({ padding: c, apply({ availableWidth: h, availableHeight: C, elements: R }) {
    Object.assign(R.floating.style, { overflow: "auto", maxWidth: `${h}px`, maxHeight: `min(var(--anchor-max-height, 100vh), ${C}px)` });
  } })].filter(Boolean), whileElementsMounted: fe }), [S = s, A = f] = m.placement.split("-");
  s === "selection" && (S = "selection");
  let K = b(() => ({ anchor: [S, A].filter(Boolean).join(" ") }), [S, A]), Q = te(m, { overflowRef: u, onChange: i }), { getReferenceProps: X, getFloatingProps: Y } = ne([Q]), Z = z((h) => {
    a(h), x.setFloating(h);
  });
  return q.createElement(k.Provider, { value: l }, q.createElement(P.Provider, { value: { setFloating: Z, setReference: x.setReference, styles: N, getReferenceProps: X, getFloatingProps: Y, slot: K } }, e));
}
function pe(e) {
  $(() => {
    if (!e) return;
    let t = new MutationObserver(() => {
      let n = window.getComputedStyle(e).maxHeight, l = parseFloat(n);
      if (isNaN(l)) return;
      let r = parseInt(n);
      isNaN(r) || l !== r && (e.style.maxHeight = `${Math.ceil(l)}px`);
    });
    return t.observe(e, { attributes: !0, attributeFilter: ["style"] }), () => {
      t.disconnect();
    };
  }, [e]);
}
function ce(e, t) {
  var n, l, r;
  let i = I((n = e == null ? void 0 : e.gap) != null ? n : "var(--anchor-gap, 0)", t), u = I((l = e == null ? void 0 : e.offset) != null ? l : "var(--anchor-offset, 0)", t), p = I((r = e == null ? void 0 : e.padding) != null ? r : "var(--anchor-padding, 0)", t);
  return { ...e, gap: i, offset: u, padding: p };
}
function I(e, t, n = void 0) {
  let l = oe(), r = z((a, o) => {
    if (a == null) return [n, null];
    if (typeof a == "number") return [a, null];
    if (typeof a == "string") {
      if (!o) return [n, null];
      let y = J(a, o);
      return [y, (E) => {
        let w = G(a);
        {
          let c = w.map((d) => window.getComputedStyle(o).getPropertyValue(d));
          l.requestAnimationFrame(function d() {
            l.nextFrame(d);
            let s = !1;
            for (let [x, N] of w.entries()) {
              let m = window.getComputedStyle(o).getPropertyValue(N);
              if (c[x] !== m) {
                c[x] = m, s = !0;
                break;
              }
            }
            if (!s) return;
            let f = J(a, o);
            y !== f && (E(f), y = f);
          });
        }
        return l.dispose;
      }];
    }
    return [n, null];
  }), i = b(() => r(e, t)[0], [e, t]), [u = i, p] = M();
  return $(() => {
    let [a, o] = r(e, t);
    if (p(a), !!o) return o(p);
  }, [e, t]), u;
}
function G(e) {
  let t = /var\((.*)\)/.exec(e);
  if (t) {
    let n = t[1].indexOf(",");
    if (n === -1) return [t[1]];
    let l = t[1].slice(0, n).trim(), r = t[1].slice(n + 1).trim();
    return r ? [l, ...G(r)] : [l];
  }
  return [];
}
function J(e, t) {
  let n = document.createElement("div");
  t.appendChild(n), n.style.setProperty("margin-top", "0px", "important"), n.style.setProperty("margin-top", e, "important");
  let l = parseFloat(window.getComputedStyle(n).marginTop) || 0;
  return t.removeChild(n), l;
}
export {
  Ce as FloatingProvider,
  we as useFloatingPanel,
  Pe as useFloatingPanelProps,
  be as useFloatingReference,
  Fe as useResolvedAnchor
};
