'use strict';

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

var vue = require('vue');
var iconsVue = require('@ant-design/icons-vue');
require('../../../utils/index.cjs');
var Notification = require('../../../vue-components/vc-notification/src/Notification.cjs');
var config = require('../../config-provider/src/config.cjs');
var index = require('../style/index.cjs');
var useNotification = require('./useNotification.cjs');
var util = require('./util.cjs');
var _class = require('../../../utils/dom/class.cjs');
var util$1 = require('../../../utils/vue/util.cjs');

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
  } = config.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 = _class.classNames(`${prefixCls}-${placement}`, {
    [`${prefixCls}-rtl`]: rtl === true
  });
  Notification.default.newInstance({
    name: "notification",
    prefixCls: customizePrefixCls || defaultPrefixCls,
    useStyle: index.default,
    class: notificationClass,
    style: util.getPlacementStyle(placement, top != null ? top : defaultTop, bottom != null ? bottom : defaultBottom),
    appContext,
    getContainer,
    closeIcon: ({
      prefixCls: prefixCls2
    }) => {
      const closeIconToRender = vue.createVNode("span", {
        "class": `${prefixCls2}-close-x`
      }, [util$1.renderHelper(closeIcon, {}, vue.createVNode(iconsVue.CloseOutlined, {
        "class": `${prefixCls2}-close-icon`
      }, null))]);
      return closeIconToRender;
    },
    maxCount,
    hasTransitionName: true
  }, (notification) => {
    notificationInstance[cacheKey] = notification;
    callback(notification);
  });
}
const typeToIcon = {
  success: iconsVue.CheckCircleOutlined,
  info: iconsVue.InfoCircleOutlined,
  error: iconsVue.CloseCircleOutlined,
  warning: iconsVue.ExclamationCircleOutlined,
  warn: iconsVue.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 = () => vue.createVNode("span", {
            "class": `${prefixCls}-icon`
          }, [util$1.renderHelper(icon)]);
        } else if (type) {
          const Icon = typeToIcon[type];
          iconNode = () => vue.createVNode(Icon, {
            "class": `${prefixCls}-icon ${prefixCls}-icon-${type}`
          }, null);
        }
        return vue.createVNode("div", {
          "class": iconNode ? `${prefixCls}-with-icon` : ""
        }, [iconNode && iconNode(), vue.createVNode("div", {
          "class": `${prefixCls}-message`
        }, [!description && iconNode ? vue.createVNode("span", {
          "class": `${prefixCls}-message-single-line-auto-margin`
        }, null) : null, util$1.renderHelper(message)]), vue.createVNode("div", {
          "class": `${prefixCls}-description`
        }, [util$1.renderHelper(description)]), btn ? vue.createVNode("span", {
          "class": `${prefixCls}-btn`
        }, [util$1.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.default;
async function getInstance(cacheKey) {
  return process.env.NODE_ENV === "test" ? notificationInstance[cacheKey] : null;
}

exports.default = api;
exports.getInstance = getInstance;
//# sourceMappingURL=Notification.cjs.map
