import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _excluded = ["prefixCls", "position", "transitionDuration"],
    _excluded2 = ["itemID", "style"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

/**
 * 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);

  var _super = _createSuper(SnackbarQueue);

  function SnackbarQueue(_props, context) {
    var _this;

    _classCallCheck(this, SnackbarQueue);

    _this = _super.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(_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, _excluded);

      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 /*#__PURE__*/React.createElement("div", {
        className: classes
      }, /*#__PURE__*/React.createElement(TransitionGroup, {
        appear: true
      }, list.map(function (_ref2) {
        var itemID = _ref2.itemID,
            style = _ref2.style,
            other = _objectWithoutProperties(_ref2, _excluded2);

        return /*#__PURE__*/React.createElement(CSSTransition, {
          classNames: transitionName,
          key: itemID,
          timeout: transitionDuration
        }, /*#__PURE__*/React.createElement(SnackbarItem, _extends({}, otherProps, other, {
          id: itemID,
          key: itemID,
          onClose: _this2.destroyItem
        })));
      })));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var defaultList = props.defaultList;

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

      return null;
    }
  }]);

  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;