import { j as m } from "../../../../../../../../../../../../_virtual/jsx-runtime.js";
import { useMemo as K, useRef as P, useState as g, useContext as M } from "react";
import { LayoutGroupContext as N } from "../../context/LayoutGroupContext.js";
import { useConstant as T } from "../../utils/use-constant.js";
import { useIsomorphicLayoutEffect as F } from "../../utils/use-isomorphic-effect.js";
import { PresenceChild as G } from "./PresenceChild.js";
import { usePresence as O } from "./use-presence.js";
import { onlyElements as w, getChildKey as c } from "./utils.js";
const H = ({ children: a, custom: R, initial: j = !0, onExitComplete: h, presenceAffectsLayout: v = !0, mode: f = "sync", propagate: l = !1, anchorX: k = "left" }) => {
  const [p, A] = O(l), n = K(() => w(a), [a]), o = l && !p ? [] : n.map(c), C = P(!0), x = P(n), r = T(() => /* @__PURE__ */ new Map()), [L, b] = g(n), [s, y] = g(n);
  F(() => {
    C.current = !1, x.current = n;
    for (let t = 0; t < s.length; t++) {
      const e = c(s[t]);
      o.includes(e) ? r.delete(e) : r.get(e) !== !0 && r.set(e, !1);
    }
  }, [s, o.length, o.join("-")]);
  const u = [];
  if (n !== L) {
    let t = [...n];
    for (let e = 0; e < s.length; e++) {
      const i = s[e], d = c(i);
      o.includes(d) || (t.splice(e, 0, i), u.push(i));
    }
    return f === "wait" && u.length && (t = u), y(w(t)), b(n), null;
  }
  process.env.NODE_ENV !== "production" && f === "wait" && s.length > 1 && console.warn(`You're attempting to animate multiple children within AnimatePresence, but its mode is set to "wait". This will lead to odd visual behaviour.`);
  const { forceRender: D } = M(N);
  return m.jsx(m.Fragment, { children: s.map((t) => {
    const e = c(t), i = l && !p ? !1 : n === s || o.includes(e), d = () => {
      if (r.has(e))
        r.set(e, !0);
      else
        return;
      let E = !0;
      r.forEach((I) => {
        I || (E = !1);
      }), E && (D?.(), y(x.current), l && A?.(), h && h());
    };
    return m.jsx(G, { isPresent: i, initial: !C.current || j ? void 0 : !1, custom: R, presenceAffectsLayout: v, mode: f, onExitComplete: i ? void 0 : d, anchorX: k, children: t }, e);
  }) });
};
export {
  H as AnimatePresence
};
//# sourceMappingURL=index.js.map
