import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
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 _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * notification.jsx
 *
 * @Author: jruif
 * @Date: 2017/8/16 下午6:00
 */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CSSTransition, TransitionGroup } from 'react-transition-group';
import SnackbarItem from './SnackbarItem';
import { isString } from '../util/isType';
var prefix = 'ne-snackbar';
var transitionAnimate = {
  top: "".concat(prefix, "-slide-down"),
  topLeft: "".concat(prefix, "-slide-right"),
  topRight: "".concat(prefix, "-slide-left"),
  bottom: "".concat(prefix, "-slide-up"),
  bottomLeft: "".concat(prefix, "-slide-right"),
  bottomRight: "".concat(prefix, "-slide-left")
};
/**
 * Snackbar
 * */

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

  _createClass(SnackbarQueue, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var defaultList = props.defaultList;

      if (defaultList !== state.prevList) {
        return {
          list: defaultList,
          // 存储子元素
          prevList: defaultList
        };
      }

      return null;
    }
  }]);

  function SnackbarQueue(_props, context) {
    var _this;

    _classCallCheck(this, SnackbarQueue);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(SnackbarQueue).call(this, _props, context));

    _defineProperty(_assertThisInitialized(_this), "createKey", function () {
      return SnackbarQueue.zIndex++;
    });

    _defineProperty(_assertThisInitialized(_this), "add", function (props) {
      var itemID = _this.createKey();

      var max = _this.props.max;

      if (isString(props)) {
        props = {
          content: props
        };
      }

      _this.setState(function (preState) {
        return {
          list: [_objectSpread({}, props, {
            itemID: itemID
          })].concat(_toConsumableArray(preState.list)).slice(0, max)
        };
      }); // 方便定位具体notice item调用


      return itemID;
    });

    _defineProperty(_assertThisInitialized(_this), "update", function (itemID, props) {
      if (isString(props)) {
        props = {
          content: props
        };
      }

      _this.setState(function (preState) {
        return {
          list: preState.list.map(function (item) {
            return item.itemID === itemID ? props : item;
          })
        };
      });
    });

    _defineProperty(_assertThisInitialized(_this), "destroyItem", function (itemID) {
      _this.setState(function (_ref) {
        var list = _ref.list;
        return {
          list: list.filter(function (item) {
            return item.itemID !== itemID;
          })
        };
      });
    });

    _this.state = {
      list: _props.defaultList,
      // 存储子元素
      prevList: _props.defaultList
    };
    return _this;
  }

  _createClass(SnackbarQueue, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          prefixCls = _this$props.prefixCls,
          position = _this$props.position,
          transitionDuration = _this$props.transitionDuration,
          otherProps = _objectWithoutProperties(_this$props, ["prefixCls", "position", "transitionDuration"]);

      var list = this.state.list;
      var transitionName = transitionAnimate[position];
      var classes = classNames(prefixCls, ["".concat(prefixCls, "-position-").concat(position)], ["".concat(prefixCls, "-queue-").concat(position.includes('top') ? 'top' : 'bottom')]);
      return React.createElement("div", {
        className: classes
      }, React.createElement(TransitionGroup, {
        appear: true
      }, list.map(function (_ref2) {
        var itemID = _ref2.itemID,
            style = _ref2.style,
            other = _objectWithoutProperties(_ref2, ["itemID", "style"]);

        return React.createElement(CSSTransition, {
          classNames: transitionName,
          key: itemID,
          timeout: transitionDuration
        }, React.createElement(SnackbarItem, _extends({}, otherProps, other, {
          id: itemID,
          key: itemID,
          onClose: _this2.destroyItem
        })));
      })));
    }
  }]);

  return SnackbarQueue;
}(React.Component);

_defineProperty(SnackbarQueue, "zIndex", 10000);

_defineProperty(SnackbarQueue, "propTypes", {
  prefixCls: PropTypes.string,

  /**
   * 打开时是否获取焦点
   * */
  autoFocus: PropTypes.bool,

  /**
   * 显示的位置
   * */
  position: PropTypes.oneOf(['top', 'topLeft', 'topRight', 'bottom', 'bottomLeft', 'bottomRight']),

  /**
   * @ignore
   * */
  items: PropTypes.array
});

_defineProperty(SnackbarQueue, "defaultProps", {
  prefixCls: prefix,
  position: 'top',
  autoFocus: false,
  transitionDuration: 1000,
  defaultList: [],
  max: 4
});

export default SnackbarQueue;