import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React from 'react';
import classNames from 'classnames';
import { Icons, RcNotification } from '../components';
import { getCurIcon } from '../utils/getTypedIcon';
import { getPrefixClassName } from '../config-provider/context';
var prefixCls = getPrefixClassName('message');
var msgInstance;

var getMessageIns = function getMessageIns(props, callback) {
  var customPrefix = props.prefix,
      zIndex = props.zIndex;
  prefixCls = getPrefixClassName('message', customPrefix, Message.globalProps.prefix);

  if (msgInstance && msgInstance.messageWrapperExit()) {
    return callback(msgInstance);
  }

  RcNotification.newInstance({
    prefixCls: prefixCls,
    getContainer: props.getContainer || Message.globalProps.getContainer,
    style: {
      zIndex: zIndex
    },
    maxCount: Message.globalProps.maxCount
  }, function (notification) {
    msgInstance = notification;
    callback(notification);
  });
};

var getContent = function getContent(props) {
  var type = props.type,
      content = props.content,
      icon = props.icon;
  var contentPrefixCls = "".concat(prefixCls, "-content");
  var curIcon = getCurIcon(Icons, type, true);
  var iconCls = classNames("".concat(contentPrefixCls, "-icon"), _defineProperty({}, "".concat(contentPrefixCls, "-").concat(type), !icon));
  return /*#__PURE__*/React.createElement("div", {
    className: contentPrefixCls
  }, /*#__PURE__*/React.createElement("span", {
    className: iconCls
  }, !icon ? curIcon : icon), /*#__PURE__*/React.createElement("span", null, content));
};

var commonNotice = function commonNotice(props) {
  var dur = Message.defaultProps.duration;
  var className = classNames(props.className);

  if ('duration' in Message.globalProps) {
    dur = Message.globalProps.duration;
  }

  if ('duration' in props) {
    dur = props.duration;
  }

  getMessageIns(props, function (notification) {
    notification.notice({
      content: getContent(props),
      onClose: function onClose() {
        var _props$onClose;

        (_props$onClose = props.onClose) === null || _props$onClose === void 0 ? void 0 : _props$onClose.call(props);
      },
      key: props.key,
      duration: dur,
      className: className,
      style: props.style
    });
  });
};

var defaultProps = {
  duration: 3
};
var Message = {
  defaultProps: defaultProps,
  globalProps: {}
};
var MessageApi = {
  config: function config(globalProps) {
    Message.globalProps = globalProps;
  },
  open: function open(props) {
    return commonNotice(props);
  },
  destroy: function destroy(messageKey) {
    if (!msgInstance) return;

    if (messageKey) {
      var _msgInstance = msgInstance,
          removeNotice = _msgInstance.removeNotice;
      removeNotice(messageKey);
    } else {
      var _msgInstance2 = msgInstance,
          destroy = _msgInstance2.destroy;
      destroy();
      msgInstance = null;
    }
  }
};
['success', 'error', 'warning', 'info'].forEach(function (type) {
  MessageApi[type] = function (content, duration, onClose) {
    var endArgs = {};

    if (Object.prototype.toString.call(content) === '[object Object]') {
      endArgs = content;
    } else {
      endArgs = typeof duration === 'function' ? {
        content: content,
        onClose: duration
      } : {
        content: content,
        onClose: onClose,
        duration: duration
      };
    }

    MessageApi.open(_objectSpread(_objectSpread({}, endArgs), {}, {
      type: type
    }));
  };
}); // 欺骗一下doc-typescript，自动生成md文档

var MessageDoczCheat = function MessageDoczCheat() {
  return null;
};

MessageDoczCheat.defaultProps = defaultProps;
export { MessageDoczCheat };
export default MessageApi;