'use strict';

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

var CheckCircleFilled = require('@ant-design/icons/CheckCircleFilled');
var CloseCircleFilled = require('@ant-design/icons/CloseCircleFilled');
var ExclamationCircleFilled = require('@ant-design/icons/ExclamationCircleFilled');
var InfoCircleFilled = require('@ant-design/icons/InfoCircleFilled');
var LoadingOutlined = require('@ant-design/icons/LoadingOutlined');
var RCNotification = require('@cloudtower/rc-notification');
var cs = require('classnames');
var React = require('react');
var index = require('../ConfigProvider/index.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var CheckCircleFilled__default = /*#__PURE__*/_interopDefault(CheckCircleFilled);
var CloseCircleFilled__default = /*#__PURE__*/_interopDefault(CloseCircleFilled);
var ExclamationCircleFilled__default = /*#__PURE__*/_interopDefault(ExclamationCircleFilled);
var InfoCircleFilled__default = /*#__PURE__*/_interopDefault(InfoCircleFilled);
var LoadingOutlined__default = /*#__PURE__*/_interopDefault(LoadingOutlined);
var RCNotification__default = /*#__PURE__*/_interopDefault(RCNotification);
var cs__default = /*#__PURE__*/_interopDefault(cs);
var React__namespace = /*#__PURE__*/_interopNamespace(React);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key2, value) => key2 in obj ? __defProp(obj, key2, { enumerable: true, configurable: true, writable: true, value }) : obj[key2] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
const DEFAULT_DURATION = 3;
const ConfigProviderWrapper = (props) => {
  const _a = props, { children } = _a, configProps = __objRest(_a, ["children"]);
  return /* @__PURE__ */ React__namespace.createElement(index.ConfigProvider, __spreadValues({}, configProps), children);
};
let messageInstance;
let defaultDuration = DEFAULT_DURATION;
let defaultTop;
let key = 1;
let localPrefixCls = "ant-message";
let transitionName = "move-up";
let getContainer;
let maxCount;
let rtl = false;
let globalConfigProviderProps = null;
function getKeyThenIncreaseKey() {
  return key++;
}
function setMessageConfig(options) {
  if (options.top !== void 0) {
    defaultTop = options.top;
    messageInstance = null;
  }
  if (options.duration !== void 0) {
    defaultDuration = options.duration;
  }
  if (options.prefixCls !== void 0) {
    localPrefixCls = options.prefixCls;
  }
  if (options.getContainer !== void 0) {
    getContainer = options.getContainer;
  }
  if (options.transitionName !== void 0) {
    transitionName = options.transitionName;
    messageInstance = null;
  }
  if (options.maxCount !== void 0) {
    maxCount = options.maxCount;
    messageInstance = null;
  }
  if (options.rtl !== void 0) {
    rtl = options.rtl;
  }
  if (options.configProviderProps !== void 0) {
    globalConfigProviderProps = options.configProviderProps;
    messageInstance = null;
  }
}
function getRCNotificationInstance(args, callback) {
  const prefixCls = args.prefixCls || localPrefixCls;
  if (messageInstance) {
    callback({
      prefixCls,
      instance: messageInstance
    });
    return;
  }
  RCNotification__default.default.newInstance(
    {
      prefixCls,
      transitionName,
      style: { top: defaultTop },
      // 覆盖原来的样式
      getContainer,
      maxCount
    },
    (instance) => {
      if (messageInstance) {
        callback({
          prefixCls,
          instance: messageInstance
        });
        return;
      }
      messageInstance = instance;
      callback({
        prefixCls,
        instance
      });
    }
  );
}
const typeToIcon = {
  info: InfoCircleFilled__default.default,
  success: CheckCircleFilled__default.default,
  error: CloseCircleFilled__default.default,
  warning: ExclamationCircleFilled__default.default,
  loading: LoadingOutlined__default.default
};
function getRCNoticeProps(args, prefixCls) {
  const duration = args.duration !== void 0 ? args.duration : defaultDuration;
  const IconComponent = args.type != null ? typeToIcon[args.type] : void 0;
  const messageClass = cs__default.default(`${prefixCls}-custom-content`, {
    [`${prefixCls}-${args.type}`]: args.type,
    [`${prefixCls}-rtl`]: rtl === true
  });
  const wrappedContent = globalConfigProviderProps ? /* @__PURE__ */ React__namespace.createElement(ConfigProviderWrapper, __spreadValues({}, globalConfigProviderProps), /* @__PURE__ */ React__namespace.createElement("div", { className: messageClass }, args.icon || IconComponent && /* @__PURE__ */ React__namespace.createElement(IconComponent, null), /* @__PURE__ */ React__namespace.createElement("span", null, args.content))) : /* @__PURE__ */ React__namespace.createElement("div", { className: messageClass }, args.icon || IconComponent && /* @__PURE__ */ React__namespace.createElement(IconComponent, null), /* @__PURE__ */ React__namespace.createElement("span", null, args.content));
  return {
    key: args.key,
    duration,
    style: args.style || {},
    className: args.className,
    content: wrappedContent,
    onClose: args.onClose
  };
}
function notice(args) {
  const target = args.key || key++;
  const closePromise = new Promise((resolve) => {
    const callback = () => {
      if (typeof args.onClose === "function") {
        args.onClose();
      }
      return resolve(true);
    };
    if (document.hidden) {
      return;
    }
    getRCNotificationInstance(args, ({ prefixCls, instance }) => {
      instance.notice(
        getRCNoticeProps(
          __spreadProps(__spreadValues({}, args), { key: target, onClose: callback }),
          prefixCls
        )
      );
    });
  });
  const result = () => {
    if (messageInstance) {
      messageInstance.removeNotice(target);
    }
  };
  result.then = (filled, rejected) => closePromise.then(filled, rejected);
  result.promise = closePromise;
  return result;
}
function isArgsProps(content) {
  return Object.prototype.toString.call(content) === "[object Object]" && !!content.content;
}
const api = {
  open: notice,
  config: setMessageConfig,
  destroy() {
    if (messageInstance) {
      messageInstance.destroy();
      messageInstance = null;
    }
  }
};
function attachTypeApi(originalApi, type) {
  originalApi[type] = (content, duration, onClose) => {
    if (isArgsProps(content)) {
      return originalApi.open(__spreadProps(__spreadValues({}, content), { type }));
    }
    if (typeof duration === "function") {
      onClose = duration;
      duration = void 0;
    }
    return originalApi.open({ content, duration, type, onClose });
  };
}
["success", "info", "warning", "error", "loading"].forEach(
  (type) => attachTypeApi(api, type)
);
api.warn = api.warning;
var _message = api;

exports.DEFAULT_DURATION = DEFAULT_DURATION;
exports.attachTypeApi = attachTypeApi;
exports.default = _message;
exports.getKeyThenIncreaseKey = getKeyThenIncreaseKey;
