import { __read as R, __spreadArray as O } from "../../../../../tslib/tslib.es6.es.js";
import * as o from "react";
import { useContext as G, useRef as h, cloneElement as T, Children as U, isValidElement as _ } from "react";
import { env as S } from "../../utils/process.es.js";
import { useForceUpdate as V } from "../../utils/use-force-update.es.js";
import { useIsMounted as Y } from "../../utils/use-is-mounted.es.js";
import { PresenceChild as x } from "./PresenceChild.es.js";
import { LayoutGroupContext as j } from "../../context/LayoutGroupContext.es.js";
import { useIsomorphicLayoutEffect as q } from "../../utils/use-isomorphic-effect.es.js";
import { useUnmountEffect as D } from "../../utils/use-unmount-effect.es.js";
var a = function(r) {
  return r.key || "";
};
function H(r, u) {
  r.forEach(function(i) {
    var s = a(i);
    u.set(s, i);
  });
}
function J(r) {
  var u = [];
  return U.forEach(r, function(i) {
    _(i) && u.push(i);
  }), u;
}
var ne = function(r) {
  var u = r.children, i = r.custom, s = r.initial, w = s === void 0 ? !0 : s, y = r.onExitComplete, C = r.exitBeforeEnter, g = r.presenceAffectsLayout, p = g === void 0 ? !0 : g, b = R(V(), 1), L = b[0], A = G(j).forceRender;
  A && (L = A);
  var k = Y(), l = J(u), t = l, n = /* @__PURE__ */ new Set(), f = h(t), m = h(/* @__PURE__ */ new Map()).current, d = h(!0);
  if (q(function() {
    d.current = !1, H(l, m), f.current = t;
  }), D(function() {
    d.current = !0, m.clear(), n.clear();
  }), d.current)
    return o.createElement(o.Fragment, null, t.map(function(e) {
      return o.createElement(x, { key: a(e), isPresent: !0, initial: w ? void 0 : !1, presenceAffectsLayout: p }, e);
    }));
  t = O([], R(t), !1);
  for (var v = f.current.map(a), P = l.map(a), z = v.length, E = 0; E < z; E++) {
    var I = v[E];
    P.indexOf(I) === -1 && n.add(I);
  }
  return C && n.size && (t = []), n.forEach(function(e) {
    if (P.indexOf(e) === -1) {
      var c = m.get(e);
      if (!!c) {
        var B = v.indexOf(e), F = function() {
          m.delete(e), n.delete(e);
          var K = f.current.findIndex(function(M) {
            return M.key === e;
          });
          if (f.current.splice(K, 1), !n.size) {
            if (f.current = l, k.current === !1)
              return;
            L(), y && y();
          }
        };
        t.splice(B, 0, o.createElement(x, { key: a(c), isPresent: !1, onExitComplete: F, custom: i, presenceAffectsLayout: p }, c));
      }
    }
  }), t = t.map(function(e) {
    var c = e.key;
    return n.has(c) ? e : o.createElement(x, { key: a(e), isPresent: !0, presenceAffectsLayout: p }, e);
  }), S !== "production" && C && t.length > 1 && console.warn("You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour."), o.createElement(o.Fragment, null, n.size ? t : t.map(function(e) {
    return T(e);
  }));
};
export {
  ne as AnimatePresence
};
//# sourceMappingURL=index.es.js.map
