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

//# sourceMappingURL=AnimatePresence.js.map