import { defineComponent, provide, nextTick, getCurrentInstance, onMounted, cloneVNode } from 'vue';
import '../../../utils/index.mjs';
import { useTooltipProps } from './props.mjs';
import { groupToolTipInjectKey, seekParent } from './utils.mjs';
import { useTooltip } from './method.mjs';
import { genRandomKey } from '../../../utils/rand.mjs';

const GroupTooltip = defineComponent({
  name: "ElGroupTooltip",
  props: useTooltipProps,
  setup(props, ctx) {
    const uniqueValue = `el-group-tooltip__${genRandomKey()}`;
    const instanceData = /* @__PURE__ */ new WeakMap();
    const destructionTooltipData = /* @__PURE__ */ new WeakMap();
    provide(groupToolTipInjectKey, {
      key: uniqueValue,
      instanceData,
      destructionTooltipFn
    });
    let root = null;
    let lastNode = null;
    function destructionTooltipFn(node) {
      var _a;
      (_a = destructionTooltipData.get(node)) == null ? void 0 : _a();
    }
    const eventListener = (e, eventName) => {
      const node = seekParent(e, uniqueValue, root);
      if (node === lastNode) {
        return;
      }
      if (node) {
        const tooltipData = instanceData.get(node);
        if (tooltipData.value.trigger !== eventName)
          return;
        lastNode = node;
        if (tooltipData.value) {
          const unUseTooltip = useTooltip({
            ...tooltipData.value,
            onHide: (value) => {
              var _a, _b;
              lastNode = null;
              (_b = (_a = tooltipData.value).onHide) == null ? void 0 : _b.call(_a, value);
            },
            closeUseTooltip: () => {
              lastNode = null;
            }
          }, node);
          nextTick(() => {
            tooltipData.value.__tooltipRef__.value = unUseTooltip == null ? void 0 : unUseTooltip.ref;
          });
          destructionTooltipData.set(node, unUseTooltip);
        }
      }
    };
    const instance = getCurrentInstance();
    onMounted(() => {
      var _a;
      root = (_a = instance == null ? void 0 : instance.proxy) == null ? void 0 : _a.$el;
    });
    return () => {
      const eventProps = {
        onClick: (e) => eventListener(e, "click"),
        onFocus: (e) => eventListener(e, "focus"),
        onMouseover: (e) => eventListener(e, "hover")
      };
      return cloneVNode(ctx.slots.default()[0], eventProps);
    };
  }
});

export { GroupTooltip };
//# sourceMappingURL=group-tooltip.mjs.map
