"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 _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

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

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

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

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

var _rcMotion = _interopRequireDefault(require("../components/rc-motion"));

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

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

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

var CloseLine = _components.Icons.CloseLine;

var Alert = function Alert(props) {
  var type = props.type,
      message = props.message,
      description = props.description,
      closable = props.closable,
      closeText = props.closeText,
      showIcon = props.showIcon,
      icon = props.icon,
      action = props.action,
      _props$className = props.className,
      className = _props$className === void 0 ? '' : _props$className,
      style = props.style,
      onClose = props.onClose,
      resetProps = (0, _objectWithoutProperties2.default)(props, ["type", "message", "description", "closable", "closeText", "showIcon", "icon", "action", "className", "style", "onClose"]); // state

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      isClosed = _useState2[0],
      setIsClosed = _useState2[1];

  var ref = _react.default.useRef();

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var alertCls = getPrefixClassName('alert'); // method

  var handleClose = function handleClose(evt) {
    setIsClosed(true);
    onClose === null || onClose === void 0 ? void 0 : onClose(evt);
  }; // render


  var renderIconNode = function renderIconNode(iconType) {
    var curIcon = (0, _getTypedIcon.getCurIcon)(_components.Icons, iconType, true);
    return /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(alertCls, "-icon")
    }, icon || /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(alertCls, "-icon-").concat(description ? 'desc-svg' : 'msg-svg')
    }, curIcon));
  };

  var renderActionNode = function renderActionNode() {
    return /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(alertCls, "-action")
    }, action);
  };

  var renderCloseNode = function renderCloseNode() {
    return /*#__PURE__*/_react.default.createElement("span", {
      onClick: handleClose,
      className: "".concat(alertCls, "-close")
    }, closeText ? /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(alertCls, "-close-text")
    }, closeText) :
    /*#__PURE__*/
    // '关闭'
    _react.default.createElement(CloseLine, null));
  };

  return /*#__PURE__*/_react.default.createElement(_rcMotion.default, {
    visible: !isClosed,
    motionName: "".concat(alertCls, "-motion"),
    motionAppear: false,
    motionEnter: false,
    onLeaveStart: function onLeaveStart(_ref) {
      var offsetHeight = _ref.offsetHeight;
      return {
        maxHeight: offsetHeight
      };
    }
  }, function (motion) {
    return /*#__PURE__*/_react.default.createElement("div", Object.assign({}, resetProps, {
      ref: ref,
      className: (0, _classnames.default)(alertCls, "".concat(alertCls, "-").concat(type), !!description && "".concat(alertCls, "-with-description"), className, motion.className),
      style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, style), motion.style),
      role: "alert"
    }), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(alertCls, "-content")
    }, showIcon ? renderIconNode(type) : null, /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(alertCls, "-message")
    }, message, description && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(alertCls, "-description")
    }, description))), action ? renderActionNode() : null, closable ? renderCloseNode() : null);
  });
};

Alert.displayName = 'Alert';
Alert.defaultProps = {
  type: 'info',
  showIcon: false,
  closable: false
};
var _default = Alert;
exports.default = _default;