import h from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import I from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import A from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import * as i from "react";
import { r as w } from "../../react-dom/index.mjs";
import F from "./NoticeList.mjs";
var H = /* @__PURE__ */ i.forwardRef(function(r, S) {
  var C = r.prefixCls, R = C === void 0 ? "rc-notification" : C, g = r.container, L = r.motion, s = r.maxCount, f = r.className, u = r.style, m = r.onAllRemoved, b = r.stack, k = r.renderNotifications, $ = i.useState([]), p = A($, 2), v = p[0], d = p[1], x = function(n) {
    var o, e = v.find(function(a) {
      return a.key === n;
    });
    e == null || (o = e.onClose) === null || o === void 0 || o.call(e), d(function(a) {
      return a.filter(function(c) {
        return c.key !== n;
      });
    });
  };
  i.useImperativeHandle(S, function() {
    return {
      open: function(n) {
        d(function(o) {
          var e = I(o), a = e.findIndex(function(D) {
            return D.key === n.key;
          }), c = h({}, n);
          if (a >= 0) {
            var N;
            c.times = (((N = o[a]) === null || N === void 0 ? void 0 : N.times) || 0) + 1, e[a] = c;
          } else
            c.times = 0, e.push(c);
          return s > 0 && e.length > s && (e = e.slice(-s)), e;
        });
      },
      close: function(n) {
        x(n);
      },
      destroy: function() {
        d([]);
      }
    };
  });
  var j = i.useState({}), E = A(j, 2), l = E[0], _ = E[1];
  i.useEffect(function() {
    var t = {};
    v.forEach(function(n) {
      var o = n.placement, e = o === void 0 ? "topRight" : o;
      e && (t[e] = t[e] || [], t[e].push(n));
    }), Object.keys(l).forEach(function(n) {
      t[n] = t[n] || [];
    }), _(t);
  }, [v]);
  var O = function(n) {
    _(function(o) {
      var e = h({}, o), a = e[n] || [];
      return a.length || delete e[n], e;
    });
  }, y = i.useRef(!1);
  if (i.useEffect(function() {
    Object.keys(l).length > 0 ? y.current = !0 : y.current && (m == null || m(), y.current = !1);
  }, [l]), !g)
    return null;
  var P = Object.keys(l);
  return /* @__PURE__ */ w.createPortal(/* @__PURE__ */ i.createElement(i.Fragment, null, P.map(function(t) {
    var n = l[t], o = /* @__PURE__ */ i.createElement(F, {
      key: t,
      configList: n,
      placement: t,
      prefixCls: R,
      className: f == null ? void 0 : f(t),
      style: u == null ? void 0 : u(t),
      motion: L,
      onNoticeClose: x,
      onAllNoticeRemoved: O,
      stack: b
    });
    return k ? k(o, {
      prefixCls: R,
      key: t
    }) : o;
  })), g);
});
process.env.NODE_ENV !== "production" && (H.displayName = "Notifications");
export {
  H as default
};
