var X = Object.defineProperty;
var H = Object.getOwnPropertySymbols;
var Y = Object.prototype.hasOwnProperty, _ = Object.prototype.propertyIsEnumerable;
var T = (e, n, t) => n in e ? X(e, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[n] = t, U = (e, n) => {
  for (var t in n || (n = {}))
    Y.call(n, t) && T(e, t, n[t]);
  if (H)
    for (var t of H(n))
      _.call(n, t) && T(e, t, n[t]);
  return e;
};
import { jsx as M, Fragment as q } from "react/jsx-runtime";
import * as k from "react";
import { useId as K, useRef as b, useContext as V, useInsertionEffect as J, useMemo as A, Children as N, isValidElement as O, useState as W } from "react";
import { M as Q, eT as I, u as B, eU as Z, eV as ee, a as te, eW as ne } from "./main-CxoAwMiG.js";
function j(e, n) {
  if (typeof e == "function")
    return e(n);
  e != null && (e.current = n);
}
function se(...e) {
  return (n) => {
    let t = !1;
    const r = e.map((c) => {
      const l = j(c, n);
      return !t && typeof l == "function" && (t = !0), l;
    });
    if (t)
      return () => {
        for (let c = 0; c < r.length; c++) {
          const l = r[c];
          typeof l == "function" ? l() : j(e[c], null);
        }
      };
  };
}
function oe(...e) {
  return k.useCallback(se(...e), e);
}
class re extends k.Component {
  getSnapshotBeforeUpdate(n) {
    const t = this.props.childRef.current;
    if (I(t) && n.isPresent && !this.props.isPresent && this.props.pop !== !1) {
      const r = t.offsetParent, c = I(r) && r.offsetWidth || 0, l = I(r) && r.offsetHeight || 0, u = getComputedStyle(t), s = this.props.sizeRef.current;
      s.height = parseFloat(u.height), s.width = parseFloat(u.width), s.top = t.offsetTop, s.left = t.offsetLeft, s.right = c - s.width - s.left, s.bottom = l - s.height - s.top, s.direction = u.direction;
    }
    return null;
  }
  /**
   * Required with getSnapshotBeforeUpdate to stop React complaining.
   */
  componentDidUpdate() {
  }
  render() {
    return this.props.children;
  }
}
function ie({ children: e, isPresent: n, anchorX: t, anchorY: r, root: c, pop: l }) {
  var f, p;
  const u = K(), s = b(null), x = b({
    width: 0,
    height: 0,
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    direction: "ltr"
  }), { nonce: R } = V(Q), a = (p = (f = e.props) == null ? void 0 : f.ref) != null ? p : e == null ? void 0 : e.ref, y = oe(s, a);
  return J(() => {
    const { width: d, height: h, top: g, left: w, right: v, bottom: z, direction: m } = x.current;
    if (n || l === !1 || !s.current || !d || !h)
      return;
    const E = m === "rtl", $ = t === "left" ? E ? `right: ${v}` : `left: ${w}` : E ? `left: ${w}` : `right: ${v}`, P = r === "bottom" ? `bottom: ${z}` : `top: ${g}`;
    s.current.dataset.motionPopId = u;
    const i = document.createElement("style");
    R && (i.nonce = R);
    const o = c != null ? c : document.head;
    return o.appendChild(i), i.sheet && i.sheet.insertRule(`
          [data-motion-pop-id="${u}"] {
            position: absolute !important;
            width: ${d}px !important;
            height: ${h}px !important;
            ${$}px !important;
            ${P}px !important;
          }
        `), () => {
      var C;
      (C = s.current) == null || C.removeAttribute("data-motion-pop-id"), o.contains(i) && o.removeChild(i);
    };
  }, [n]), M(re, { isPresent: n, childRef: s, sizeRef: x, pop: l, children: l === !1 ? e : k.cloneElement(e, { ref: y }) });
}
const ce = ({ children: e, initial: n, isPresent: t, onExitComplete: r, custom: c, presenceAffectsLayout: l, mode: u, anchorX: s, anchorY: x, root: R }) => {
  const a = B(le), y = K();
  let f = !0, p = A(() => (f = !1, {
    id: y,
    initial: n,
    isPresent: t,
    custom: c,
    onExitComplete: (d) => {
      a.set(d, !0);
      for (const h of a.values())
        if (!h)
          return;
      r && r();
    },
    register: (d) => (a.set(d, !1), () => a.delete(d))
  }), [t, a, r]);
  return l && f && (p = U({}, p)), A(() => {
    a.forEach((d, h) => a.set(h, !1));
  }, [t]), k.useEffect(() => {
    !t && !a.size && r && r();
  }, [t]), e = M(ie, { pop: u === "popLayout", isPresent: t, anchorX: s, anchorY: x, root: R, children: e }), M(Z.Provider, { value: p, children: e });
};
function le() {
  return /* @__PURE__ */ new Map();
}
const L = (e) => e.key || "";
function D(e) {
  const n = [];
  return N.forEach(e, (t) => {
    O(t) && n.push(t);
  }), n;
}
const de = ({ children: e, custom: n, initial: t = !0, onExitComplete: r, presenceAffectsLayout: c = !0, mode: l = "sync", propagate: u = !1, anchorX: s = "left", anchorY: x = "top", root: R }) => {
  const [a, y] = ee(u), f = A(() => D(e), [e]), p = u && !a ? [] : f.map(L), d = b(!0), h = b(f), g = B(() => /* @__PURE__ */ new Map()), w = b(/* @__PURE__ */ new Set()), [v, z] = W(f), [m, E] = W(f);
  te(() => {
    d.current = !1, h.current = f;
    for (let i = 0; i < m.length; i++) {
      const o = L(m[i]);
      p.includes(o) ? (g.delete(o), w.current.delete(o)) : g.get(o) !== !0 && g.set(o, !1);
    }
  }, [m, p.length, p.join("-")]);
  const $ = [];
  if (f !== v) {
    let i = [...f];
    for (let o = 0; o < m.length; o++) {
      const C = m[o], S = L(C);
      p.includes(S) || (i.splice(o, 0, C), $.push(C));
    }
    return l === "wait" && $.length && (i = $), E(D(i)), z(f), null;
  }
  const { forceRender: P } = V(ne);
  return M(q, { children: m.map((i) => {
    const o = L(i), C = u && !a ? !1 : f === m || p.includes(o), S = () => {
      if (w.current.has(o))
        return;
      if (g.has(o))
        w.current.add(o), g.set(o, !0);
      else
        return;
      let F = !0;
      g.forEach((G) => {
        G || (F = !1);
      }), F && (P == null || P(), E(h.current), u && (y == null || y()), r && r());
    };
    return M(ce, { isPresent: C, initial: !d.current || t ? void 0 : !1, custom: n, presenceAffectsLayout: c, mode: l, root: R, onExitComplete: C ? void 0 : S, anchorX: s, anchorY: x, children: i }, o);
  }) });
};
export {
  de as A
};
