"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toast = exports.ToastManager = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/toConsumableArray"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/createClass"));
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _Toast = require("./Toast");
require("../styles/toaster.css");
var ToastManager = /*#__PURE__*/function () {
  function ToastManager() {
    (0, _classCallCheck2.default)(this, ToastManager);
    this.containerRef = null;
    this.toasts = [];
    var body = document.getElementsByTagName("body")[0];
    var toastContainer = document.createElement("div");
    toastContainer.id = "toast-container-main";
    body.insertAdjacentElement("beforeend", toastContainer);
    this.containerRef = toastContainer;
  }
  (0, _createClass2.default)(ToastManager, [{
    key: "show",
    value: function show(options) {
      var _this = this;
      var toastId = Math.random().toString(36).substr(2, 9);
      var toast = (0, _objectSpread2.default)((0, _objectSpread2.default)({
        id: toastId
      }, options), {}, {
        // if id is passed within options, it will overwrite the auto-generated one
        destroy: function destroy() {
          var _options$id;
          return _this.destroy((_options$id = options.id) !== null && _options$id !== void 0 ? _options$id : toastId);
        }
      });
      this.toasts = [toast].concat((0, _toConsumableArray2.default)(this.toasts));
      this.render();
    }
  }, {
    key: "destroy",
    value: function destroy(id) {
      this.toasts = this.toasts.filter(function (toast) {
        return toast.id !== id;
      });
      this.render();
    }
  }, {
    key: "render",
    value: function render() {
      var toastsList = this.toasts.map(function (toastProps) {
        return /*#__PURE__*/_react.default.createElement(_Toast.Toast, Object.assign({
          key: toastProps.id
        }, toastProps));
      });
      _reactDom.default.render(toastsList, this.containerRef);
    }
  }]);
  return ToastManager;
}();
exports.ToastManager = ToastManager;
var toast = new ToastManager();
exports.toast = toast;