'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
var index = require('../../../components/config-provider/index.cjs');
require('../../transition/index.cjs');
var Notice = require('./Notice.cjs');
var transition = require('../../transition/src/transition.cjs');
var _class = require('../../../utils/dom/class.cjs');
var config = require('../../../components/config-provider/src/config.cjs');

let seed = 0;
const now = Date.now();
function getUuid() {
  const id = seed;
  seed += 1;
  return `rcNotification_${now}_${id}`;
}
const Notification = /* @__PURE__ */ vue.defineComponent({
  name: "Notification",
  inheritAttrs: false,
  props: ["prefixCls", "transitionName", "animation", "maxCount", "closeIcon", "hashId"],
  setup(props, {
    attrs,
    expose,
    slots
  }) {
    const hookRefs = /* @__PURE__ */ new Map();
    const notices = vue.ref([]);
    const transitionProps = vue.computed(() => {
      const {
        prefixCls,
        animation = "fade"
      } = props;
      let name = props.transitionName;
      if (!name && animation)
        name = `${prefixCls}-${animation}`;
      return transition.getTransitionGroupProps(name);
    });
    const add = (originNotice, holderCallback) => {
      const key = originNotice.key || getUuid();
      const notice = {
        ...originNotice,
        key
      };
      const {
        maxCount
      } = props;
      const noticeIndex = notices.value.map((v) => v.notice.key).indexOf(key);
      const updatedNotices = notices.value.concat();
      if (noticeIndex !== -1) {
        updatedNotices.splice(noticeIndex, 1, {
          notice,
          holderCallback
        });
      } else {
        if (maxCount && notices.value.length >= maxCount) {
          notice.key = updatedNotices[0].notice.key;
          notice.updateMark = getUuid();
          notice.userPassKey = key;
          updatedNotices.shift();
        }
        updatedNotices.push({
          notice,
          holderCallback
        });
      }
      notices.value = updatedNotices;
    };
    const remove = (removeKey) => {
      notices.value = notices.value.filter(({
        notice: {
          key,
          userPassKey
        }
      }) => {
        const mergedKey = userPassKey || key;
        return mergedKey !== removeKey;
      });
    };
    expose({
      add,
      remove,
      notices
    });
    return () => {
      var _a;
      const {
        prefixCls,
        closeIcon = (_a = slots.closeIcon) == null ? void 0 : _a.call(slots, {
          prefixCls
        })
      } = props;
      const noticeNodes = notices.value.map(({
        notice,
        holderCallback
      }, index) => {
        const updateMark = index === notices.value.length - 1 ? notice.updateMark : void 0;
        const {
          key,
          userPassKey
        } = notice;
        const {
          content
        } = notice;
        const noticeProps = {
          prefixCls,
          closeIcon: typeof closeIcon === "function" ? closeIcon({
            prefixCls
          }) : closeIcon,
          ...notice,
          ...notice.props,
          key,
          noticeKey: userPassKey || key,
          updateMark,
          onClose: (noticeKey) => {
            var _a2;
            remove(noticeKey);
            (_a2 = notice.onClose) == null ? void 0 : _a2.call(notice);
          },
          onClick: notice.onClick
        };
        if (holderCallback) {
          return vue.createVNode("div", {
            "key": key,
            "class": `${prefixCls}-hook-holder`,
            "ref": (div) => {
              if (typeof key === "undefined")
                return;
              if (div) {
                hookRefs.set(key, div);
                holderCallback(div, noticeProps);
              } else {
                hookRefs.delete(key);
              }
            }
          }, null);
        }
        return vue.createVNode(Notice.default, {
          ...noticeProps,
          "class": _class.classNames(noticeProps.class, props.hashId)
        }, {
          default: () => [typeof content === "function" ? content({
            prefixCls
          }) : content]
        });
      });
      const className = {
        [prefixCls]: 1,
        [attrs.class]: !!attrs.class,
        [props.hashId]: true
      };
      return vue.createVNode("div", {
        "class": className,
        "style": attrs.style || {
          top: "65px",
          left: "50%"
        }
      }, [vue.createVNode(vue.TransitionGroup, {
        "tag": "div",
        ...transitionProps.value
      }, {
        default: () => [noticeNodes]
      })]);
    };
  }
});
Notification.newInstance = function newNotificationInstance(properties, callback) {
  const {
    name = "notification",
    getContainer,
    appContext,
    prefixCls: customizePrefixCls,
    rootPrefixCls: customRootPrefixCls,
    transitionName: customTransitionName,
    hasTransitionName,
    useStyle,
    ...props
  } = properties || {};
  const div = document.createElement("div");
  if (getContainer) {
    const root = getContainer();
    root.appendChild(div);
  } else {
    document.body.appendChild(div);
  }
  const Wrapper = /* @__PURE__ */ vue.defineComponent({
    compatConfig: {
      MODE: 3
    },
    name: "NotificationWrapper",
    setup(_props, {
      attrs
    }) {
      const notiRef = vue.shallowRef();
      const prefixCls = vue.computed(() => config.globalConfigForApi.getPrefixCls(name, customizePrefixCls));
      const [, hashId] = useStyle(prefixCls);
      vue.onMounted(() => {
        callback({
          notice(noticeProps) {
            var _a;
            (_a = notiRef.value) == null ? void 0 : _a.add(noticeProps);
          },
          removeNotice(key) {
            var _a;
            (_a = notiRef.value) == null ? void 0 : _a.remove(key);
          },
          destroy() {
            vue.render(null, div);
            if (div.parentNode)
              div.parentNode.removeChild(div);
          },
          component: notiRef
        });
      });
      return () => {
        const global = config.globalConfigForApi;
        const rootPrefixCls = global.getRootPrefixCls(customRootPrefixCls, prefixCls.value);
        const transitionName = hasTransitionName ? customTransitionName : `${prefixCls.value}-${customTransitionName}`;
        return vue.createVNode(index.ConfigProvider, {
          ...global,
          "prefixCls": rootPrefixCls
        }, {
          default: () => [vue.createVNode(Notification, {
            "ref": notiRef,
            ...attrs,
            "prefixCls": prefixCls.value,
            "transitionName": transitionName,
            "hashId": hashId.value
          }, null)]
        });
      };
    }
  });
  const vm = vue.createVNode(Wrapper, props);
  vm.appContext = appContext || vm.appContext;
  vue.render(vm, div);
};
var VcNotification = Notification;

exports.default = VcNotification;
//# sourceMappingURL=Notification.cjs.map
