import { createRef } from "react";
import ContextHolderElement from "../_util/contextHolder.js";
import Notification from "./index.js";
import { isUndefined } from "../_util/is.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
function useNotification(commonConfig) {
  if (commonConfig === void 0) {
    commonConfig = {};
  }
  var maxCount = commonConfig.maxCount, _a = commonConfig.duration, duration = _a === void 0 ? 3e3 : _a, _prefixCls = commonConfig.prefixCls;
  var contextHolderRef = createRef();
  var holderEle = /* @__PURE__ */ jsx(ContextHolderElement, {
    ref: contextHolderRef
  });
  var notificationInstance = {};
  var notice;
  function addNotice(noticeProps) {
    var prefixCls, rtl;
    if (contextHolderRef.current) {
      var contextConfig = contextHolderRef.current.getContextConfig();
      rtl = contextConfig.rtl;
      prefixCls = contextConfig.prefixCls;
    }
    var mergedPrefixCls = _prefixCls || prefixCls;
    var position = noticeProps.position;
    if (isUndefined(noticeProps.position)) {
      position = rtl ? "topLeft" : "topRight";
    }
    var _noticeProps = __assign({
      duration
    }, noticeProps);
    var id;
    if (notificationInstance[position]) {
      var notices = notificationInstance[position].state.notices;
      if (notices.length >= maxCount) {
        var updated = notices[0];
        id = updated.id;
        notices.shift();
        notificationInstance[position].add(__assign(__assign({}, _noticeProps), {
          id
        }));
      } else {
        id = notificationInstance[position].add(_noticeProps);
      }
    } else {
      notice = /* @__PURE__ */ jsx(Notification, {
        ref: function(instance) {
          notificationInstance[position] = instance;
          if (notificationInstance[position]) {
            id = notificationInstance[position].add(_noticeProps);
          }
        },
        prefixCls: mergedPrefixCls,
        rtl
      });
      contextHolderRef.current.addInstance(notice);
    }
    return notificationInstance[position];
  }
  var notificationFuncs = {};
  ["info", "success", "warning", "error", "normal"].forEach(function(type) {
    notificationFuncs[type] = function(config) {
      return addNotice(__assign(__assign({}, config), {
        type
      }));
    };
  });
  return [notificationFuncs, holderEle];
}
export {
  useNotification as default
};
//# sourceMappingURL=useNotification.js.map
