"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.MessageDoczCheat = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _components = require("../components");

var _getTypedIcon = require("../utils/getTypedIcon");

var _context = require("../config-provider/context");

var prefixCls = (0, _context.getPrefixClassName)('message');
var msgInstance;

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

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

  _components.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 = (0, _getTypedIcon.getCurIcon)(_components.Icons, type, true);
  var iconCls = (0, _classnames.default)("".concat(contentPrefixCls, "-icon"), (0, _defineProperty2.default)({}, "".concat(contentPrefixCls, "-").concat(type), !icon));
  return /*#__PURE__*/_react.default.createElement("div", {
    className: contentPrefixCls
  }, /*#__PURE__*/_react.default.createElement("span", {
    className: iconCls
  }, !icon ? curIcon : icon), /*#__PURE__*/_react.default.createElement("span", null, content));
};

var commonNotice = function commonNotice(props) {
  var dur = Message.defaultProps.duration;
  var className = (0, _classnames.default)(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((0, _objectSpread2.default)((0, _objectSpread2.default)({}, endArgs), {}, {
      type: type
    }));
  };
}); // 欺骗一下doc-typescript，自动生成md文档

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

exports.MessageDoczCheat = MessageDoczCheat;
MessageDoczCheat.defaultProps = defaultProps;
var _default = MessageApi;
exports.default = _default;