import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * SnackbarItem.jsx
 *
 * @Author: jruif
 * @Date: 2018/1/17 上午9:48
 */
import React, { isValidElement, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import EventListener from 'react-event-listener'; // import Icon from '../icon';

import Close from '../icons/internals/Close';
import statusIcons from '../icons/statusIcons';
import IconButton from '../button/IconButton';
/**
 * SnackbarItem
 * */

var SnackbarItem =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(SnackbarItem, _React$PureComponent);

  function SnackbarItem() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, SnackbarItem);

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

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

    _defineProperty(_assertThisInitialized(_this), "setTimer", function (duration) {
      _this.timer = setTimeout(_this.close, duration);
    });

    _defineProperty(_assertThisInitialized(_this), "restartTimeout", function () {
      var autoHideDuration = _this.props.autoHideDuration;

      if (autoHideDuration > 0) {
        _this.setTimer(autoHideDuration * 0.5);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "startTimeout", function () {
      var autoHideDuration = _this.props.autoHideDuration;

      if (autoHideDuration > 0) {
        _this.setTimer(autoHideDuration);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "clearTimeout", function () {
      clearTimeout(_this.timer);
    });

    _defineProperty(_assertThisInitialized(_this), "close", function (e) {
      var _this$props = _this.props,
          onClose = _this$props.onClose,
          id = _this$props.id;
      onClose && onClose(id, e ? 'click' : 'timeout');

      _this.clearTimeout();
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseEnter", function (e) {
      var onMouseEnter = _this.props.onMouseEnter;

      if (onMouseEnter) {
        onMouseEnter(e);
      }

      _this.clearTimeout();
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseLeave", function (e) {
      var onMouseLeave = _this.props.onMouseLeave;

      if (onMouseLeave) {
        onMouseLeave(e);
      }

      _this.restartTimeout();
    });

    return _this;
  }

  _createClass(SnackbarItem, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.startTimeout();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.clearTimeout();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          className = _this$props2.className,
          children = _this$props2.children,
          content = _this$props2.content,
          actionsRender = _this$props2.actionsRender,
          style = _this$props2.style,
          hideCloseButton = _this$props2.hideCloseButton,
          title = _this$props2.title,
          icon = _this$props2.icon,
          disableWindowBlurListener = _this$props2.disableWindowBlurListener,
          id = _this$props2.id,
          type = _this$props2.type;
      var classes = classNames(prefixCls, "".concat(prefixCls, "-color-").concat(type), className);
      var StatusIcon = statusIcons[type];
      return React.createElement("div", {
        className: classes,
        onMouseEnter: this.handleMouseEnter,
        onMouseLeave: this.handleMouseLeave,
        style: Object.assign({
          zIndex: id
        }, style)
      }, React.createElement(EventListener, {
        target: "window",
        onFocus: disableWindowBlurListener ? undefined : this.restartTimeout,
        onBlur: disableWindowBlurListener ? undefined : this.clearTimeout
      }), React.createElement("div", {
        className: "".concat(prefixCls, "-body")
      }, React.createElement("div", {
        className: classNames("".concat(prefixCls, "-content-container"), _defineProperty({}, "".concat(prefixCls, "-content-container-full-width"), title))
      }, icon && isValidElement(icon) ? cloneElement(icon, {
        className: classNames("".concat(prefixCls, "-icon"), icon.props.className)
      }) : StatusIcon ? React.createElement(StatusIcon, {
        className: "".concat(prefixCls, "-icon")
      }) : null, React.createElement("div", {
        className: "".concat(prefixCls, "-wrapper")
      }, title ? React.createElement("h6", {
        className: "".concat(prefixCls, "-title")
      }, title) : null, React.createElement("div", {
        className: "".concat(prefixCls, "-description")
      }, content || children))), actionsRender ? React.createElement("div", {
        className: "".concat(prefixCls, "-actions")
      }, actionsRender(this.close)) : null, hideCloseButton ? null : React.createElement(IconButton, {
        className: classNames("".concat(prefixCls, "-close")),
        size: "small",
        variant: type === 'primary' ? 'hollow' : 'text',
        shape: "borderless",
        onClick: this.close
      }, React.createElement(Close, null))));
    }
  }]);

  return SnackbarItem;
}(React.PureComponent);

_defineProperty(SnackbarItem, "displayName", 'NeatUI.SnackbarItem');

_defineProperty(SnackbarItem, "propTypes", _defineProperty({
  /**
   * 默认 5 秒后自动关闭，配置为 0 时则不自动关闭
   * */
  autoHideDuration: PropTypes.number,

  /**
   * 需要显示的内容，接受的类型是PropTypes.node，即下列类型均可渲染: numbers, strings, elements 或一个 array
   * */
  content: PropTypes.node.isRequired,

  /**
   * 自定义行为按钮，必须返回Element
   * */
  actionsRender: PropTypes.func,

  /**
   * 关闭时回调
   * */
  onClose: PropTypes.func,

  /**
   * 类型
   * */
  type: PropTypes.oneOf(['info', 'error', 'warning', 'success', 'none']),

  /**
   * 是否显示close按钮
   * */
  hideCloseButton: PropTypes.bool,

  /**
   * icon类型, 同Icon
   * */
  icon: PropTypes.node
}, "type", PropTypes.oneOf(['primary', 'success', 'warning', 'info', 'error'])));

_defineProperty(SnackbarItem, "defaultProps", {
  autoHideDuration: 5000,
  prefixCls: 'ne-snackbar-item',
  showClose: true,
  type: 'primary',
  hideCloseButton: false
});

export default SnackbarItem;