function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/* eslint-disable no-script-url */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import nextLocale from '../locale/zh-cn';
import Icon from '../icon';
import Animate from '../animate';
import Button from '../button';
import ConfigProvider from '../config-provider';

var noop = function noop() {};

var shapeToIcon = {
  success: 'tick-o-fill',
  warning: 'warning-o-fill',
  notice: 'info-o-fill',
  loading: 'loading',
  error: 'remove-o-fill'
};
/**
 * Message
 */

var Message =
/*#__PURE__*/
function (_Component) {
  _inherits(Message, _Component);

  function Message() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Message);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Message)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      visible: typeof _this.props.visible === 'undefined' ? _this.props.defaultVisible : _this.props.visible
    });

    _defineProperty(_assertThisInitialized(_this), "onClose", function () {
      if (!('visible' in _this.props)) {
        _this.setState({
          visible: false
        });
      }

      _this.props.onClose(false);
    });

    _defineProperty(_assertThisInitialized(_this), "globalButton", function (messagePrefix) {
      var _this$props = _this.props,
          type = _this$props.type,
          onClickHandler = _this$props.onClickHandler,
          handler = _this$props.handler;
      var cls = "".concat(messagePrefix, "-handler");

      switch (type) {
        case 'error':
          return React.createElement(Button, {
            className: cls,
            type: "normal",
            size: "xs",
            warning: true,
            onClick: onClickHandler
          }, handler);

        case 'help':
          return React.createElement(Button, {
            className: cls,
            type: "primary",
            size: "xs",
            onClick: onClickHandler
          }, handler);

        default:
          return React.createElement(Button, {
            className: cls,
            type: "normal",
            size: "xs",
            onClick: onClickHandler
          }, handler);
      }
    });

    return _this;
  }

  _createClass(Message, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('visible' in nextProps) {
        this.setState({
          visible: nextProps.visible
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      /* eslint-disable no-unused-vars */
      var _this$props2 = this.props,
          prefix = _this$props2.prefix,
          pure = _this$props2.pure,
          className = _this$props2.className,
          type = _this$props2.type,
          shape = _this$props2.shape,
          size = _this$props2.size,
          title = _this$props2.title,
          children = _this$props2.children,
          defaultVisible = _this$props2.defaultVisible,
          propsVisible = _this$props2.visible,
          icon = _this$props2.iconType,
          handler = _this$props2.handler,
          closeable = _this$props2.closeable,
          onClose = _this$props2.onClose,
          afterClose = _this$props2.afterClose,
          onClickHandler = _this$props2.onClickHandler,
          animation = _this$props2.animation,
          rtl = _this$props2.rtl,
          locale = _this$props2.locale,
          others = _objectWithoutProperties(_this$props2, ["prefix", "pure", "className", "type", "shape", "size", "title", "children", "defaultVisible", "visible", "iconType", "handler", "closeable", "onClose", "afterClose", "onClickHandler", "animation", "rtl", "locale"]);
      /* eslint-enable */


      var visible = this.state.visible;
      var messagePrefix = "".concat(prefix, "message");
      var classes = classNames((_classNames = {}, _defineProperty(_classNames, messagePrefix, true), _defineProperty(_classNames, "".concat(prefix, "message-").concat(type), type), _defineProperty(_classNames, "".concat(prefix).concat(shape), shape), _defineProperty(_classNames, "".concat(prefix).concat(size), size && shape !== 'global'), _defineProperty(_classNames, "".concat(prefix, "title-content"), !!title), _defineProperty(_classNames, "".concat(prefix, "only-content"), !title && !!children && shape !== 'global'), _defineProperty(_classNames, className, className), _classNames));
      var content = shape === 'global' ? React.createElement("div", {
        className: "".concat(messagePrefix, "-content-wrapper")
      }, children ? React.createElement("div", {
        className: "".concat(messagePrefix, "-content")
      }, children) : null, handler ? this.globalButton(messagePrefix) : null) : React.createElement("div", {
        className: "".concat(messagePrefix, "-content-wrapper")
      }, React.createElement("div", {
        className: "".concat(messagePrefix, "-title-wrapper")
      }, title ? React.createElement("div", {
        className: "".concat(messagePrefix, "-title")
      }, title) : null, handler ? React.createElement(Button, {
        className: "".concat(messagePrefix, "-handler"),
        text: true,
        type: "primary",
        onClick: onClickHandler
      }, handler) : null), children ? React.createElement("div", {
        className: "".concat(messagePrefix, "-content")
      }, children) : null);
      var newChildren = visible ? React.createElement("div", _extends({
        role: "alert"
      }, others, {
        className: classes,
        dir: rtl ? 'rtl' : undefined
      }), shape !== 'global' ? React.createElement(Icon, {
        className: "".concat(messagePrefix, "-symbol ").concat(!icon && "".concat(messagePrefix, "-symbol-icon")),
        type: icon || shapeToIcon[type] || 'info-o-fill'
      }) : null, content, closeable ? React.createElement("a", {
        role: "button",
        "aria-label": locale.closeAriaLabel,
        href: "javascript:;",
        className: "".concat(messagePrefix, "-close"),
        onClick: this.onClose
      }, React.createElement(Icon, {
        type: "remove"
      })) : null) : null;

      if (animation) {
        return React.createElement(Animate.Expand, {
          animationAppear: false,
          afterLeave: afterClose
        }, newChildren);
      }

      return newChildren;
    }
  }]);

  return Message;
}(Component);

_defineProperty(Message, "displayName", 'Message');

_defineProperty(Message, "propTypes", {
  prefix: PropTypes.string,
  pure: PropTypes.bool,
  className: PropTypes.string,
  style: PropTypes.object,

  /**
   * 反馈类型
   */
  type: PropTypes.oneOf(['success', 'warning', 'error', 'notice', 'help', 'loading']),

  /**
   * 反馈外观
   */
  shape: PropTypes.oneOf(['inline', 'addon', 'toast', 'global']),

  /**
   * 反馈大小
   */
  size: PropTypes.oneOf(['medium', 'large']),

  /**
   * 标题
   */
  title: PropTypes.node,

  /**
   * 内容
   */
  children: PropTypes.node,

  /**
   * 默认是否显示
   */
  defaultVisible: PropTypes.bool,

  /**
   * 当前是否显示
   */
  visible: PropTypes.bool,

  /**
   * 显示的图标类型，会覆盖内部设置的IconType
   */
  iconType: PropTypes.string,

  /**
   * 操作行动点，文字或者 JSX.Element
   */
  handler: PropTypes.string,

  /**
   * 显示关闭按钮
   */
  closeable: PropTypes.bool,

  /**
   * 关闭按钮的回调
   */
  onClose: PropTypes.func,

  /**
   * 关闭之后调用的函数
   */
  afterClose: PropTypes.func,

  /**
   * 点击行动点的回掉
   */
  onClickHandler: PropTypes.func,

  /**
   * 是否开启展开收起动画
   */
  animation: PropTypes.bool,
  locale: PropTypes.object,
  rtl: PropTypes.bool
});

_defineProperty(Message, "defaultProps", {
  prefix: 'next-',
  pure: false,
  type: 'success',
  shape: 'inline',
  size: 'medium',
  defaultVisible: true,
  closeable: false,
  onClose: noop,
  afterClose: noop,
  animation: true,
  locale: nextLocale.Message
});

export default ConfigProvider.config(Message);