import _extends from "@babel/runtime/helpers/esm/extends";
import React from 'react';
import ReactDOM from 'react-dom';
import SnackbarQueue from './SnackbarQueue';
var defaultProps = {};
/**
 * Snackbar
 * */

var Snackbar = {
  queues: {},
  createQueue: function createQueue(props, callback) {
    var root = document.body;
    var containerElement = document.createElement('div'); // 不在DOM中创建节点

    root.appendChild(containerElement);
    var mounted = false;

    function getQueue(queue) {
      if (mounted) {
        return;
      }

      mounted = true;
      callback && callback({
        queue: queue,
        destroy: function destroy() {
          ReactDOM.unmountComponentAtNode(containerElement);
          containerElement.parentNode.removeChild(containerElement);
        }
      });
    }

    ReactDOM.render(React.createElement(SnackbarQueue, _extends({}, props, {
      ref: getQueue
    })), containerElement);
  },
  open: function open() {
    var itemProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultProps;
    var position = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'top';

    if (this.queues[position]) {
      this.queues[position].queue.add(itemProps);
    } else {
      this.createQueue({
        position: position
      }, function (queue) {
        Snackbar.queues[position] = queue;
        queue.queue.add(itemProps);
      });
    }
  },
  config: function config(newConfig) {
    Object.assign(defaultProps, newConfig);
  },
  destroy: function destroy() {
    var _this = this;

    Object.keys(this.queues).forEach(function (key) {
      _this.queues[key].destroy();

      delete _this.queues[key];
    });
  }
};
export default Snackbar;