"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

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

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

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

var React = _interopRequireWildcard(require("react"));

var ReactDOM = _interopRequireWildcard(require("react-dom"));

var _rcAnimate = _interopRequireDefault(require("rc-animate"));

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

var _Close = _interopRequireDefault(require("../icon/Close"));

var _CheckCircle = _interopRequireDefault(require("../icon/CheckCircle"));

var _ExclamationCircle = _interopRequireDefault(require("../icon/ExclamationCircle"));

var _InfoCircle = _interopRequireDefault(require("../icon/InfoCircle"));

var _CloseCircle = _interopRequireDefault(require("../icon/CloseCircle"));

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

var _getDataOrAriaProps = _interopRequireDefault(require("../_util/getDataOrAriaProps"));

var _ErrorBoundary = _interopRequireDefault(require("./ErrorBoundary"));

var iconMapOutlined = {
  success: _CheckCircle["default"],
  info: _InfoCircle["default"],
  error: _CloseCircle["default"],
  warning: _ExclamationCircle["default"]
};

var Alert = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Alert, _React$Component);

  var _super = (0, _createSuper2["default"])(Alert);

  function Alert() {
    var _this;

    (0, _classCallCheck2["default"])(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((0, _assertThisInitialized2["default"])(_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 = (0, _classnames["default"])(prefixCls, "".concat(prefixCls, "-").concat(type), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-grey-background"), greyBackground), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-no-background"), noBackground), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-closing"), closing), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-with-description"), !!description), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-no-icon"), !isShowIcon), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-banner"), !!banner), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-closable"), closable), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className);

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

      var dataOrAriaProps = (0, _getDataOrAriaProps["default"])(_this.props);

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

      return closed ? null : /*#__PURE__*/React.createElement(_rcAnimate["default"], {
        component: "",
        showProp: "data-show",
        transitionName: "".concat(prefixCls, "-slide-up"),
        onEnd: _this.animationEnd
      }, /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({
        "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;
  }

  (0, _createClass2["default"])(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: (0, _classnames["default"])("".concat(prefixCls, "-icon"), (0, _defineProperty2["default"])({}, 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(_Close["default"], null)) : null;
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(_configProvider.ConfigConsumer, null, this.renderAlert);
    }
  }]);
  return Alert;
}(React.Component);

exports["default"] = Alert;
Alert.ErrorBoundary = _ErrorBoundary["default"];