"use strict";

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});

var jsx_runtime_1 = require("react/jsx-runtime");

var rc_notification_1 = __importDefault(require("rc-notification"));

var ToastContainer_1 = __importDefault(require("./ToastContainer"));

require("./style/toast.css");

var maxCount = 6;
var prefixCls = 'dmc-toast';
var toastConfig = {
  withArrow: false,
  withClose: false,
  duration: 3
};
var notification;
var ps;

var newNotification = function newNotification() {
  if (notification) return Promise.resolve(notification);

  if (!notification && ps) {
    return ps;
  }

  return ps = new Promise(function (resolve) {
    rc_notification_1["default"].newInstance({
      prefixCls: prefixCls,
      maxCount: maxCount
    }, function (n) {
      notification = n;
      ps = null;
      resolve(n);
    });
  });
};

var i = 0; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

var toastApi = {};

toastApi.config = function (config) {
  toastConfig = __assign(__assign({}, toastConfig), config);
};

['success', 'warning', 'info', 'error'].forEach(function (key) {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  toastApi[key] = function (message, config) {
    var id = "toast-" + ++i;
    var insPs = newNotification().then(function (n) {
      var props = typeof message === 'string' ? {
        message: message
      } : message;

      var onClose = function onClose() {
        return n.removeNotice(id);
      };

      n.notice(__assign(__assign({
        content: jsx_runtime_1.jsx(ToastContainer_1["default"], __assign({
          type: key
        }, toastConfig, props, {
          onClose: onClose
        }), void 0),
        key: id,
        closeIcon: false,
        closable: false
      }, toastConfig), config || {}));
      return n;
    });
    return {
      id: id,
      notificationPromise: insPs
    };
  };
});
exports["default"] = toastApi;

