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