import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import RCNotification from '@cloudtower/rc-notification';
import cs from 'classnames';
import * as React from '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));
const DEFAULT_DURATION = 3;
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;
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;
  }
}
function getRCNotificationInstance(args, callback) {
  const prefixCls = args.prefixCls || localPrefixCls;
  if (messageInstance) {
    callback({
      prefixCls,
      instance: messageInstance
    });
    return;
  }
  RCNotification.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,
  success: CheckCircleFilled,
  error: CloseCircleFilled,
  warning: ExclamationCircleFilled,
  loading: LoadingOutlined
};
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(`${prefixCls}-custom-content`, {
    [`${prefixCls}-${args.type}`]: args.type,
    [`${prefixCls}-rtl`]: rtl === true
  });
  return {
    key: args.key,
    duration,
    style: args.style || {},
    className: args.className,
    content: /* @__PURE__ */ React.createElement("div", { className: messageClass }, args.icon || IconComponent && /* @__PURE__ */ React.createElement(IconComponent, null), /* @__PURE__ */ React.createElement("span", null, args.content)),
    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;

export { DEFAULT_DURATION, attachTypeApi, _message as default, getKeyThenIncreaseKey };
