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