import "react";
import { render } from "../_util/react-dom.js";
import BaseNotification from "../_class/notification.js";
import Notice from "../_class/notice.js";
import cs from "../_util/classNames.js";
import { isUndefined } from "../_util/is.js";
import useMessage from "./useMessage.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
import TransitionGroup from "../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/TransitionGroup.js";
import CSSTransition from "../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/CSSTransition.js";
var __extends = globalThis && globalThis.__extends || function() {
  var extendStatics = function(d, b) {
    extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function(d2, b2) {
      d2.__proto__ = b2;
    } || function(d2, b2) {
      for (var p in b2)
        if (Object.prototype.hasOwnProperty.call(b2, p))
          d2[p] = b2[p];
    };
    return extendStatics(d, b);
  };
  return function(d, b) {
    if (typeof b !== "function" && b !== null)
      throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
    extendStatics(d, b);
    function __() {
      this.constructor = d;
    }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();
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);
};
var messageTypes = ["info", "success", "error", "warning", "loading", "normal"];
var messageInstance = {};
var maxCount;
var prefixCls;
var duration;
var container;
var rtl;
function addInstance(noticeProps) {
  var _noticeProps = __assign({
    position: "top",
    duration
  }, noticeProps);
  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 {
    var div = document.createElement("div");
    (container || document.body).appendChild(div);
    render(/* @__PURE__ */ jsx(Message, {
      transitionClassNames,
      ref: function(instance) {
        messageInstance[position] = instance;
        id = messageInstance[position].add(_noticeProps);
      }
    }), div);
  }
  var result = function() {
    if (messageInstance[position]) {
      messageInstance[position].remove(id);
    }
  };
  return result;
}
var Message = function(_super) {
  __extends(Message2, _super);
  function Message2() {
    var _this = _super !== null && _super.apply(this, arguments) || this;
    _this.remove = function(id) {
      var noticeItem = _this.state.notices.find(function(item) {
        return item.id === id;
      });
      if (noticeItem) {
        _this.update(__assign(__assign({}, noticeItem), {
          style: __assign(__assign({}, noticeItem.style), {
            opacity: 0
          })
        }));
      }
      setTimeout(function() {
        _super.prototype.remove.call(_this, id);
      }, 100);
    };
    return _this;
  }
  Message2.prototype.render = function() {
    var _this = this;
    var _a = this.props, transitionClassNames = _a.transitionClassNames, _prefixCls = _a.prefixCls, _rtl = _a.rtl;
    var _b = this.state, notices = _b.notices, position = _b.position;
    var mergedPrefixCls = _prefixCls || prefixCls;
    var mergedRtl = !isUndefined(_rtl) ? _rtl : rtl;
    var prefixClsMessage = mergedPrefixCls ? mergedPrefixCls + "-message" : "arco-message";
    var classNames = cs(prefixClsMessage + "-wrapper", prefixClsMessage + "-wrapper-" + position);
    return /* @__PURE__ */ jsx("div", {
      className: classNames,
      children: /* @__PURE__ */ jsx(TransitionGroup, {
        component: null,
        children: notices.map(function(notice) {
          return /* @__PURE__ */ jsx(CSSTransition, {
            timeout: {
              enter: 100,
              exit: 300
            },
            classNames: transitionClassNames || "fadeMessage",
            onExit: function(e) {
              e.style.height = e.scrollHeight + "px";
            },
            onExiting: function(e) {
              e.style.height = 0;
            },
            onExited: function(e) {
              e.style.height = 0;
              notice.onClose && notice.onClose();
            },
            children: /* @__PURE__ */ jsx(Notice, {
              ...__assign({}, notice, {
                prefixCls: prefixClsMessage,
                iconPrefix: mergedPrefixCls,
                onClose: _this.remove,
                noticeType: "message",
                rtl: mergedRtl
              })
            })
          }, notice.id);
        })
      })
    });
  };
  Message2.config = function(options) {
    if (options === void 0) {
      options = {};
    }
    if (options.maxCount) {
      maxCount = options.maxCount;
    }
    if (options.prefixCls) {
      prefixCls = options.prefixCls;
    }
    if (options.duration) {
      duration = options.duration;
    }
    if (options.rtl) {
      rtl = options.rtl;
    }
    if (options.getContainer && options.getContainer() !== container) {
      container = options.getContainer();
      Object.keys(messageInstance).forEach(function(notice) {
        return messageInstance[notice].clear();
      });
      messageInstance = {};
    }
  };
  Message2.clear = function() {
    Object.keys(messageInstance).forEach(function(ins) {
      messageInstance[ins].clear();
    });
  };
  Message2.addInstance = addInstance;
  return Message2;
}(BaseNotification);
messageTypes.forEach(function(type) {
  Message[type] = function(noticeProps) {
    var props = typeof noticeProps === "string" ? {
      content: noticeProps
    } : noticeProps;
    return addInstance(__assign(__assign({}, props), {
      type
    }));
  };
});
Message.useMessage = useMessage;
const Message$1 = Message;
export {
  Message$1 as default
};
//# sourceMappingURL=index.js.map
