import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Animate from 'rc-animate';
import classNames from 'classnames';
import CloseOutlined from '../icon/Close';
import CheckCircleOutlined from '../icon/CheckCircle';
import ExclamationCircleOutlined from '../icon/ExclamationCircle';
import InfoCircleOutlined from '../icon/InfoCircle';
import CloseCircleOutlined from '../icon/CloseCircle';
import { ConfigConsumer } from '../config-provider';
import getDataOrAriaProps from '../_util/getDataOrAriaProps';
import ErrorBoundary from './ErrorBoundary';
var iconMapOutlined = {
  success: CheckCircleOutlined,
  info: InfoCircleOutlined,
  error: CloseCircleOutlined,
  warning: ExclamationCircleOutlined
};

var Alert = /*#__PURE__*/function (_React$Component) {
  _inherits(Alert, _React$Component);

  var _super = _createSuper(Alert);

  function Alert() {
    var _this;

    _classCallCheck(this, Alert);

    _this = _super.apply(this, arguments);
    _this.state = {
      closing: false,
      closed: false
    };

    _this.handleClose = function (e) {
      var _a, _b;

      e.preventDefault();
      var dom = ReactDOM.findDOMNode(_assertThisInitialized(_this));
      dom.style.height = "".concat(dom.offsetHeight, "px"); // Magic code
      // 重复一次后才能正确设置 height

      dom.style.height = "".concat(dom.offsetHeight, "px");

      _this.setState({
        closing: true
      });

      (_b = (_a = _this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, e);
    };

    _this.animationEnd = function () {
      var _a, _b;

      _this.setState({
        closing: false,
        closed: true
      });

      (_b = (_a = _this.props).afterClose) === null || _b === void 0 ? void 0 : _b.call(_a);
    };

    _this.renderAlert = function (_ref) {
      var _classNames;

      var getPrefixCls = _ref.getPrefixCls,
          direction = _ref.direction;
      var _this$props = _this.props,
          description = _this$props.description,
          customizePrefixCls = _this$props.prefixCls,
          message = _this$props.message,
          banner = _this$props.banner,
          _this$props$className = _this$props.className,
          className = _this$props$className === void 0 ? '' : _this$props$className,
          style = _this$props.style,
          onMouseEnter = _this$props.onMouseEnter,
          onMouseLeave = _this$props.onMouseLeave,
          onClick = _this$props.onClick,
          greyBackground = _this$props.greyBackground,
          noBackground = _this$props.noBackground;
      var _this$state = _this.state,
          closing = _this$state.closing,
          closed = _this$state.closed;
      var prefixCls = getPrefixCls('alert', customizePrefixCls);

      var isShowIcon = _this.getShowIcon();

      var type = _this.getType();

      var closable = _this.getClosable();

      var alertCls = classNames(prefixCls, "".concat(prefixCls, "-").concat(type), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-grey-background"), greyBackground), _defineProperty(_classNames, "".concat(prefixCls, "-no-background"), noBackground), _defineProperty(_classNames, "".concat(prefixCls, "-closing"), closing), _defineProperty(_classNames, "".concat(prefixCls, "-with-description"), !!description), _defineProperty(_classNames, "".concat(prefixCls, "-no-icon"), !isShowIcon), _defineProperty(_classNames, "".concat(prefixCls, "-banner"), !!banner), _defineProperty(_classNames, "".concat(prefixCls, "-closable"), closable), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className);

      var closeIcon = _this.renderCloseIcon({
        prefixCls: prefixCls
      });

      var dataOrAriaProps = getDataOrAriaProps(_this.props);

      var iconNode = _this.renderIconNode({
        prefixCls: prefixCls
      });

      return closed ? null : /*#__PURE__*/React.createElement(Animate, {
        component: "",
        showProp: "data-show",
        transitionName: "".concat(prefixCls, "-slide-up"),
        onEnd: _this.animationEnd
      }, /*#__PURE__*/React.createElement("div", _extends({
        "data-show": !closing,
        className: alertCls,
        style: style,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        onClick: onClick
      }, dataOrAriaProps), isShowIcon ? iconNode : null, /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-message")
      }, message), /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-description")
      }, description), closeIcon));
    };

    return _this;
  }

  _createClass(Alert, [{
    key: "getShowIcon",
    value: function getShowIcon() {
      var _this$props2 = this.props,
          banner = _this$props2.banner,
          showIcon = _this$props2.showIcon; // banner 模式默认有 Icon

      return banner && showIcon === undefined ? true : showIcon;
    }
  }, {
    key: "getType",
    value: function getType() {
      var _this$props3 = this.props,
          banner = _this$props3.banner,
          type = _this$props3.type;

      if (type !== undefined) {
        return type;
      } // banner 模式默认为警告


      return banner ? 'warning' : 'info';
    }
  }, {
    key: "getClosable",
    value: function getClosable() {
      var _this$props4 = this.props,
          closable = _this$props4.closable,
          closeText = _this$props4.closeText; // closeable when closeText is assigned

      return closeText ? true : closable;
    }
  }, {
    key: "getIconType",
    value: function getIconType() {
      // use outline icon in alert
      return iconMapOutlined[this.getType()] || null;
    }
  }, {
    key: "renderIconNode",
    value: function renderIconNode(_ref2) {
      var prefixCls = _ref2.prefixCls;
      var icon = this.props.icon;
      var iconType = this.getIconType();

      if (icon) {
        return /*#__PURE__*/React.isValidElement(icon) ? /*#__PURE__*/React.cloneElement(icon, {
          className: classNames("".concat(prefixCls, "-icon"), _defineProperty({}, icon.props.className, icon.props.className))
        }) : /*#__PURE__*/React.createElement("span", {
          className: "".concat(prefixCls, "-icon")
        }, icon);
      }

      return /*#__PURE__*/React.createElement(iconType, {
        className: "".concat(prefixCls, "-icon")
      });
    }
  }, {
    key: "renderCloseIcon",
    value: function renderCloseIcon(_ref3) {
      var prefixCls = _ref3.prefixCls;
      var closeText = this.props.closeText;
      return this.getClosable() ? /*#__PURE__*/React.createElement("button", {
        type: "button",
        onClick: this.handleClose,
        className: "".concat(prefixCls, "-close-icon"),
        tabIndex: 0
      }, closeText ? /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-close-text")
      }, closeText) : /*#__PURE__*/React.createElement(CloseOutlined, null)) : null;
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(ConfigConsumer, null, this.renderAlert);
    }
  }]);

  return Alert;
}(React.Component);

export { Alert as default };
Alert.ErrorBoundary = ErrorBoundary;