import { createVNode } from 'vue';
import { CloseOutlined, CheckCircleOutlined, InfoCircleOutlined, CloseCircleOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue';
import '../../../utils/index.mjs';
import VcNotification from '../../../vue-components/vc-notification/src/Notification.mjs';
import { globalConfig } from '../../config-provider/src/config.mjs';
import useStyle from '../style/index.mjs';
import useNotification from './useNotification.mjs';
import { getPlacementStyle } from './util.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import { renderHelper } from '../../../utils/vue/util.mjs';

const notificationInstance = {};
let defaultDuration = 4.5;
let defaultTop = "24px";
let defaultBottom = "24px";
let defaultPrefixCls = "";
let defaultPlacement = "topRight";
let defaultGetContainer = () => document.body;
let defaultCloseIcon = null;
let rtl = false;
let maxCount;
function setNotificationConfig(options) {
  const {
    duration,
    placement,
    bottom,
    top,
    getContainer,
    closeIcon,
    prefixCls
  } = options;
  if (prefixCls !== void 0)
    defaultPrefixCls = prefixCls;
  if (duration !== void 0)
    defaultDuration = duration;
  if (placement !== void 0)
    defaultPlacement = placement;
  if (bottom !== void 0)
    defaultBottom = typeof bottom === "number" ? `${bottom}px` : bottom;
  if (top !== void 0)
    defaultTop = typeof top === "number" ? `${top}px` : top;
  if (getContainer !== void 0)
    defaultGetContainer = getContainer;
  if (closeIcon !== void 0)
    defaultCloseIcon = closeIcon;
  if (options.rtl !== void 0)
    rtl = options.rtl;
  if (options.maxCount !== void 0)
    maxCount = options.maxCount;
}
function getNotificationInstance({
  prefixCls: customizePrefixCls,
  placement = defaultPlacement,
  getContainer = defaultGetContainer,
  top,
  bottom,
  closeIcon = defaultCloseIcon,
  appContext
}, callback) {
  const {
    getPrefixCls
  } = globalConfig();
  const prefixCls = getPrefixCls("notification", customizePrefixCls || defaultPrefixCls);
  const cacheKey = `${prefixCls}-${placement}-${rtl}`;
  const cacheInstance = notificationInstance[cacheKey];
  if (cacheInstance) {
    Promise.resolve(cacheInstance).then((instance) => {
      callback(instance);
    });
    return;
  }
  const notificationClass = classNames(`${prefixCls}-${placement}`, {
    [`${prefixCls}-rtl`]: rtl === true
  });
  VcNotification.newInstance({
    name: "notification",
    prefixCls: customizePrefixCls || defaultPrefixCls,
    useStyle,
    class: notificationClass,
    style: getPlacementStyle(placement, top != null ? top : defaultTop, bottom != null ? bottom : defaultBottom),
    appContext,
    getContainer,
    closeIcon: ({
      prefixCls: prefixCls2
    }) => {
      const closeIconToRender = createVNode("span", {
        "class": `${prefixCls2}-close-x`
      }, [renderHelper(closeIcon, {}, createVNode(CloseOutlined, {
        "class": `${prefixCls2}-close-icon`
      }, null))]);
      return closeIconToRender;
    },
    maxCount,
    hasTransitionName: true
  }, (notification) => {
    notificationInstance[cacheKey] = notification;
    callback(notification);
  });
}
const typeToIcon = {
  success: CheckCircleOutlined,
  info: InfoCircleOutlined,
  error: CloseCircleOutlined,
  warning: ExclamationCircleOutlined,
  warn: ExclamationCircleOutlined
};
function notice(args) {
  const {
    icon,
    type,
    description,
    message,
    btn
  } = args;
  const duration = args.duration === void 0 ? defaultDuration : args.duration;
  getNotificationInstance(args, (notification) => {
    notification.notice({
      content: ({
        prefixCls: outerPrefixCls
      }) => {
        const prefixCls = `${outerPrefixCls}-notice`;
        let iconNode = null;
        if (icon) {
          iconNode = () => createVNode("span", {
            "class": `${prefixCls}-icon`
          }, [renderHelper(icon)]);
        } else if (type) {
          const Icon = typeToIcon[type];
          iconNode = () => createVNode(Icon, {
            "class": `${prefixCls}-icon ${prefixCls}-icon-${type}`
          }, null);
        }
        return createVNode("div", {
          "class": iconNode ? `${prefixCls}-with-icon` : ""
        }, [iconNode && iconNode(), createVNode("div", {
          "class": `${prefixCls}-message`
        }, [!description && iconNode ? createVNode("span", {
          "class": `${prefixCls}-message-single-line-auto-margin`
        }, null) : null, renderHelper(message)]), createVNode("div", {
          "class": `${prefixCls}-description`
        }, [renderHelper(description)]), btn ? createVNode("span", {
          "class": `${prefixCls}-btn`
        }, [renderHelper(btn)]) : null]);
      },
      duration,
      closable: true,
      onClose: args.onClose,
      onClick: args.onClick,
      key: args.key,
      style: args.style || {},
      class: args.class
    });
  });
}
const api = {
  open: notice,
  close(key) {
    Object.keys(notificationInstance).forEach((cacheKey) => Promise.resolve(notificationInstance[cacheKey]).then((instance) => {
      instance.removeNotice(key);
    }));
  },
  config: setNotificationConfig,
  destroy() {
    Object.keys(notificationInstance).forEach((cacheKey) => {
      Promise.resolve(notificationInstance[cacheKey]).then((instance) => {
        instance.destroy();
      });
      delete notificationInstance[cacheKey];
    });
  }
};
const iconTypes = ["success", "info", "warning", "warn", "error"];
iconTypes.forEach((type) => {
  api[type] = (args) => api.open({
    ...args,
    type
  });
});
api.useNotification = useNotification;
async function getInstance(cacheKey) {
  return process.env.NODE_ENV === "test" ? notificationInstance[cacheKey] : null;
}

export { api as default, getInstance };
//# sourceMappingURL=Notification.mjs.map
