function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { unmountComponentAtNode, render } from 'react-dom';

var ServiceManager =
/*#__PURE__*/
function () {
  function ServiceManager(queue) {
    var _this = this;

    var tag = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'span';

    _classCallCheck(this, ServiceManager);

    _defineProperty(this, "handleClose", function (key) {
      _this.queue["delete"](key);
    });

    this.queue = queue;
    this.tag = tag;
  }

  _createClass(ServiceManager, [{
    key: "close",
    value: function close(key) {
      this.handleClose(key);
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.queue.deleteAll();

      if (this.el) {
        unmountComponentAtNode(this.el);
        document.body.removeChild(this.el);
        this.el = null;
      }
    }
  }, {
    key: "createSlice",
    value: function createSlice(key, duration) {
      var _this2 = this;

      var timeoutKey;

      var startTimeout = function startTimeout() {
        if (typeof duration === 'number' && duration !== 0) {
          timeoutKey = window.setTimeout(function () {
            _this2.handleClose(key);
          }, duration);
        }
      };

      var clearTimeout = function clearTimeout() {
        if (timeoutKey) {
          window.clearTimeout(timeoutKey);
        }
      };

      startTimeout();
      return {
        clearTimeout: clearTimeout,
        startTimeout: startTimeout
      };
    }
  }, {
    key: "createMountNode",
    value: function createMountNode(tag) {
      var el = document.createElement(tag);
      document.body.appendChild(el);
      return el;
    }
  }, {
    key: "flush",
    value: function flush(component, callback) {
      render(component, this.mountNode, callback);
    }
  }, {
    key: "mountNode",
    get: function get() {
      if (this.el) {
        return this.el;
      }

      var node = this.createMountNode(this.tag);
      this.el = node;
      return node;
    }
  }]);

  return ServiceManager;
}();

export { ServiceManager as default };