import { defineComponent, computed, ref, watch, createVNode, TransitionGroup } from 'vue';
import '../../../utils/index.mjs';
import Portal from '../../portal/src/Portal.mjs';
import '../../transition/index.mjs';
import VcNotice from './Notice.mjs';
import { getTransitionGroupProps } from '../../transition/src/transition.mjs';
import { classNames } from '../../../utils/dom/class.mjs';

let seed = 0;
const now = Date.now();
function getUuid() {
  const id = seed;
  seed += 1;
  return `rcNotification_${now}_${id}`;
}
const Notification = /* @__PURE__ */ defineComponent({
  name: "HookNotification",
  inheritAttrs: false,
  props: ["prefixCls", "transitionName", "animation", "maxCount", "closeIcon", "hashId", "remove", "notices", "getStyles", "getClassName", "onAllRemoved", "getContainer"],
  setup(props, {
    attrs,
    slots
  }) {
    const hookRefs = /* @__PURE__ */ new Map();
    const notices = computed(() => props.notices);
    const transitionProps = computed(() => {
      let name = props.transitionName;
      if (!name && props.animation) {
        switch (typeof props.animation) {
          case "string":
            name = props.animation;
            break;
          case "function":
            name = props.animation().name;
            break;
          case "object":
            name = props.animation.name;
            break;
          default:
            name = `${props.prefixCls}-fade`;
            break;
        }
      }
      return getTransitionGroupProps(name);
    });
    const remove = (key) => props.remove(key);
    const placements = ref({});
    watch(notices, () => {
      const nextPlacements = {};
      Object.keys(placements.value).forEach((placement) => {
        nextPlacements[placement] = [];
      });
      props.notices.forEach((config) => {
        const {
          placement = "topRight"
        } = config.notice;
        if (placement) {
          nextPlacements[placement] = nextPlacements[placement] || [];
          nextPlacements[placement].push(config);
        }
      });
      placements.value = nextPlacements;
    });
    const placementList = computed(() => Object.keys(placements.value));
    return () => {
      var _a;
      const {
        prefixCls,
        closeIcon = (_a = slots.closeIcon) == null ? void 0 : _a.call(slots, {
          prefixCls
        })
      } = props;
      const noticeNodes = placementList.value.map((placement) => {
        var _a2, _b;
        const noticesForPlacement = placements.value[placement];
        const classes = (_a2 = props.getClassName) == null ? void 0 : _a2.call(props, placement);
        const styles = (_b = props.getStyles) == null ? void 0 : _b.call(props, placement);
        const noticeNodesForPlacement = noticesForPlacement.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 _a3;
              remove(noticeKey);
              (_a3 = notice.onClose) == null ? void 0 : _a3.call(notice);
            },
            onClick: notice.onClick
          };
          if (holderCallback) {
            return 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 createVNode(VcNotice, {
            ...noticeProps,
            "class": classNames(noticeProps.class, props.hashId)
          }, {
            default: () => [typeof content === "function" ? content({
              prefixCls
            }) : content]
          });
        });
        const className = {
          [prefixCls]: 1,
          [`${prefixCls}-${placement}`]: 1,
          [attrs.class]: !!attrs.class,
          [props.hashId]: true,
          [classes]: !!classes
        };
        function onAfterLeave() {
          var _a3;
          if (noticesForPlacement.length > 0)
            return;
          Reflect.deleteProperty(placements.value, placement);
          (_a3 = props.onAllRemoved) == null ? void 0 : _a3.call(props);
        }
        return createVNode("div", {
          "key": placement,
          "class": className,
          "style": attrs.style || styles || {
            top: "65px",
            left: "50%"
          }
        }, [createVNode(TransitionGroup, {
          "tag": "div",
          ...transitionProps.value,
          "onAfterLeave": onAfterLeave
        }, {
          default: () => [noticeNodesForPlacement]
        })]);
      });
      return createVNode(Portal, {
        "getContainer": props.getContainer
      }, {
        default: () => [noticeNodes]
      });
    };
  }
});

export { Notification as default, getUuid };
//# sourceMappingURL=HookNotification.mjs.map
