import { defineComponent, computed, ref, watch, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../vue-components/vc-tooltip/index.mjs';
import '../../../vue-components/transition/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import { parseColor } from './util.mjs';
import { tooltipProps } from './props.mjs';
import { initDefaultProps } from '../../../utils/vue/initDefaultProps.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { firstNotUndefined } from '../../../utils/lang/firstNotUndefined.mjs';
import { raf } from '../../../utils/vue/raf.mjs';
import { getPlacements } from '../../../utils/placement.mjs';
import { getStyle, filterEmpty } from '../../../utils/vue/props.mjs';
import { cloneElement } from '../../../utils/vue/vnode.mjs';
import { isValidElement, isFragment } from '../../../utils/is.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import { getTransitionName } from '../../../vue-components/transition/src/transition.mjs';
import VcTooltip from '../../../vue-components/vc-tooltip/src/Tooltip.mjs';

function splitObject(obj, keys) {
  const picked = {};
  const omitted = {
    ...obj
  };
  keys.forEach((key) => {
    if (obj && key in obj) {
      picked[key] = obj[key];
      delete omitted[key];
    }
  });
  return {
    picked,
    omitted
  };
}
var AToolTip = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ATooltip",
  inheritAttrs: false,
  props: initDefaultProps(tooltipProps(), {
    trigger: "hover",
    align: {},
    placement: "top",
    mouseEnterDelay: 0.1,
    mouseLeaveDelay: 0.1,
    arrowPointAtCenter: false,
    autoAdjustOverflow: true
  }),
  slots: Object,
  // emits: ['update:visible', 'visibleChange'],
  setup(props, {
    slots,
    emit,
    attrs,
    expose
  }) {
    if (process.env.NODE_ENV !== "production") {
      [["visible", "open"], ["onVisibleChange", "onOpenChange"]].forEach(([deprecatedName, newName]) => {
        devWarning(props[deprecatedName] === void 0, "Tooltip", `\`${deprecatedName}\` is deprecated, please use \`${newName}\` instead.`);
      });
    }
    const {
      prefixCls,
      getPopupContainer,
      direction,
      rootPrefixCls
    } = useConfigInject("tooltip", props);
    const mergedOpen = computed(() => {
      var _a;
      return (_a = props.open) != null ? _a : props.visible;
    });
    const innerOpen = ref(firstNotUndefined([props.open, props.visible]));
    const tooltip = ref();
    let rafId;
    watch(mergedOpen, (val) => {
      raf.cancel(rafId);
      rafId = raf(() => {
        innerOpen.value = !!val;
      });
    });
    const isNoTitle = () => {
      var _a;
      const title = (_a = props.title) != null ? _a : slots.title;
      return !title && title !== 0;
    };
    const handleVisibleChange = (val) => {
      const noTitle = isNoTitle();
      if (mergedOpen.value === void 0)
        innerOpen.value = noTitle ? false : val;
      if (!noTitle) {
        emit("update:visible", val);
        emit("visibleChange", val);
        emit("update:open", val);
        emit("openChange", val);
      }
    };
    const getPopupDomNode = () => {
      return tooltip.value.getPopupDomNode();
    };
    expose({
      getPopupDomNode,
      open: innerOpen,
      forcePopupAlign: () => {
        var _a;
        return (_a = tooltip.value) == null ? void 0 : _a.forcePopupAlign();
      }
    });
    const tooltipPlacements = computed(() => {
      const {
        builtinPlacements,
        arrowPointAtCenter,
        autoAdjustOverflow
      } = props;
      return builtinPlacements || getPlacements({
        arrowPointAtCenter,
        autoAdjustOverflow
      });
    });
    const isTrueProps = (val) => {
      return val || val === "";
    };
    const getDisabledCompatibleChildren = (ele) => {
      const elementType = ele.type;
      if (typeof elementType === "object" && ele.props) {
        if ((elementType.__ANT_BUTTON === true || elementType === "button") && isTrueProps(ele.props.disabled) || elementType.__ANT_SWITCH === true && (isTrueProps(ele.props.disabled) || isTrueProps(ele.props.loading)) || elementType.__ANT_RADIO === true && isTrueProps(ele.props.disabled)) {
          const {
            picked,
            omitted
          } = splitObject(getStyle(ele), ["position", "left", "right", "top", "bottom", "float", "display", "zIndex"]);
          const spanStyle = {
            display: "inline-block",
            // default inline-block is important
            ...picked,
            cursor: "not-allowed",
            lineHeight: 1,
            // use the true height of child nodes
            width: ele.props && ele.props.block ? "100%" : void 0
          };
          const buttonStyle = {
            ...omitted,
            pointerEvents: "none"
          };
          const child = cloneElement(ele, {
            style: buttonStyle
          }, true);
          return createVNode("span", {
            "style": spanStyle,
            "class": `${prefixCls.value}-disabled-compatible-wrapper`
          }, [child]);
        }
      }
      return ele;
    };
    const getOverlay = () => {
      var _a, _b;
      return (_b = props.title) != null ? _b : (_a = slots.title) == null ? void 0 : _a.call(slots);
    };
    const onPopupAlign = (domNode, align) => {
      const placements = tooltipPlacements.value;
      const placement = Object.keys(placements).find((key) => {
        var _a, _b;
        return placements[key].points[0] === ((_a = align.points) == null ? void 0 : _a[0]) && placements[key].points[1] === ((_b = align.points) == null ? void 0 : _b[1]);
      });
      if (placement) {
        const rect = domNode.getBoundingClientRect();
        const transformOrigin = {
          top: "50%",
          left: "50%"
        };
        if (placement.includes("top") || placement.includes("Bottom"))
          transformOrigin.top = `${rect.height - align.offset[1]}px`;
        else if (placement.includes("Top") || placement.includes("bottom"))
          transformOrigin.top = `${-align.offset[1]}px`;
        if (placement.includes("left") || placement.includes("Right"))
          transformOrigin.left = `${rect.width - align.offset[0]}px`;
        else if (placement.includes("right") || placement.includes("Left"))
          transformOrigin.left = `${-align.offset[0]}px`;
        domNode.style.transformOrigin = `${transformOrigin.left} ${transformOrigin.top}`;
      }
    };
    const colorInfo = computed(() => parseColor(prefixCls.value, props.color));
    const injectFromPopover = computed(() => attrs["data-popover-inject"]);
    const [wrapSSR, hashId] = useStyle(prefixCls, computed(() => !injectFromPopover.value));
    return () => {
      var _a, _b;
      const {
        openClassName,
        overlayClassName,
        overlayStyle,
        overlayInnerStyle
      } = props;
      let children = (_b = filterEmpty((_a = slots.default) == null ? void 0 : _a.call(slots))) != null ? _b : null;
      children = children.length === 1 ? children[0] : children;
      let tempVisible = innerOpen.value;
      if (mergedOpen.value === void 0 && isNoTitle())
        tempVisible = false;
      if (!children)
        return null;
      const child = getDisabledCompatibleChildren(isValidElement(children) && !isFragment(children) ? children : createVNode("span", null, [children]));
      const childCls = classNames({
        [openClassName || `${prefixCls.value}-open`]: true,
        [child.props && child.props.class]: child.props && child.props.class
      });
      const customOverlayClassName = classNames(overlayClassName, {
        [`${prefixCls.value}-rtl`]: direction.value === "rtl"
      }, colorInfo.value.className, hashId.value);
      const formattedOverlayInnerStyle = {
        ...colorInfo.value.overlayStyle,
        ...overlayInnerStyle
      };
      const arrowContentStyle = colorInfo.value.arrowStyle;
      const vcTooltipProps = {
        ...attrs,
        ...props,
        prefixCls: prefixCls.value,
        getPopupContainer: getPopupContainer == null ? void 0 : getPopupContainer.value,
        builtinPlacements: tooltipPlacements.value,
        visible: tempVisible,
        ref: tooltip,
        overlayClassName: customOverlayClassName,
        overlayStyle: {
          ...arrowContentStyle,
          ...overlayStyle
        },
        overlayInnerStyle: formattedOverlayInnerStyle,
        onVisibleChange: handleVisibleChange,
        onPopupAlign,
        transitionName: getTransitionName(rootPrefixCls.value, "zoom-big-fast", props.transitionName)
      };
      return wrapSSR(createVNode(VcTooltip, vcTooltipProps, {
        default: () => [innerOpen.value ? cloneElement(child, {
          class: childCls
        }) : child],
        arrowContent: () => createVNode("span", {
          "class": `${prefixCls.value}-arrow-content`
        }, null),
        overlay: getOverlay
      }));
    };
  }
});

export { AToolTip as default };
//# sourceMappingURL=Tooltip.mjs.map
