"use strict";

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

exports.__esModule = true;
exports.NotificationDispatcher = void 0;

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _lodash = _interopRequireDefault(require("lodash"));

var NotificationDispatcher =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(NotificationDispatcher, _React$Component);

  function NotificationDispatcher() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      stack: []
    };
    return _this;
  }

  var _proto = NotificationDispatcher.prototype;

  _proto.getChildContext = function getChildContext() {
    return {
      isTopFixedNotification: true
    };
  };

  _proto.showNotification = function showNotification(element) {
    var _this2 = this;

    var key = Date.now();

    var notification = _react.default.cloneElement(element, {
      key: key,
      onClose: function onClose() {
        _this2.onNotificationClose(key);

        if (element.props.onClose) {
          element.props.onClose();
        }
      }
    });

    return new Promise(function (resolve) {
      var stack = [{
        notification: notification,
        key: key
      }].concat(_this2.state.stack);

      _this2.setState({
        stack: stack
      }, resolve);
    });
  };

  _proto.onNotificationClose = function onNotificationClose(key) {
    var index = _lodash.default.findIndex(this.state.stack, {
      key: key
    });

    if (index !== -1) {
      this.setState(function (state) {
        var stack = [].concat(state.stack);
        stack.splice(index, 1);
        return {
          stack: stack
        };
      });
    }
  };

  _proto.render = function render() {
    return _react.default.createElement("div", null, this.state.stack.map(function (_ref) {
      var notification = _ref.notification;
      return notification;
    }));
  };

  return NotificationDispatcher;
}(_react.default.Component);

exports.NotificationDispatcher = NotificationDispatcher;
(0, _defineProperty2.default)(NotificationDispatcher, "childContextTypes", {
  isTopFixedNotification: _propTypes.default.bool
});