import { createRef } from "react";
import ContextHolderElement from "../_util/contextHolder.js";
import Message from "./index.js";
import { isString } 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 useMessage(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 messageInstance = {};
  var notice;
  function addNotice(config) {
    var prefixCls, rtl;
    if (contextHolderRef.current) {
      var contextConfig = contextHolderRef.current.getContextConfig();
      rtl = contextConfig.rtl;
      prefixCls = contextConfig.prefixCls;
    }
    var mergedPrefixCls = _prefixCls || prefixCls;
    var _noticeProps = __assign({
      position: "top",
      duration
    }, config);
    var position = _noticeProps.position, transitionClassNames = _noticeProps.transitionClassNames;
    var id;
    if (messageInstance[position]) {
      var notices = messageInstance[position].state.notices;
      if (notices.length >= maxCount) {
        var updated = notices[0];
        id = updated.id;
        notices.shift();
        messageInstance[position].add(__assign(__assign({}, _noticeProps), {
          id
        }));
      } else {
        id = messageInstance[position].add(_noticeProps);
      }
    } else {
      notice = /* @__PURE__ */ jsx(Message, {
        transitionClassNames,
        ref: function(instance) {
          messageInstance[position] = instance;
          if (messageInstance[position]) {
            id = messageInstance[position].add(_noticeProps);
          }
        },
        prefixCls: mergedPrefixCls,
        rtl
      });
      contextHolderRef.current.addInstance(notice);
    }
    var close = function() {
      if (messageInstance[position]) {
        messageInstance[position].remove(id);
      }
    };
    return close;
  }
  var messageFuncs = {};
  ["info", "success", "warning", "error", "normal"].forEach(function(type) {
    messageFuncs[type] = function(config) {
      var _config = isString(config) ? {
        content: config
      } : config;
      return addNotice(__assign(__assign({}, _config), {
        type
      }));
    };
  });
  return [messageFuncs, holderEle];
}
export {
  useMessage as default
};
//# sourceMappingURL=useMessage.js.map
