import { createVNode as _createVNode } from "vue";
import { normalizeClass } from "@vue/shared";
import { defineComponent, watch, reactive, getCurrentInstance } from "vue";
import { getComponent } from '../_util/props-util';
import Trigger from '../trigger';
import placements from './placements';
import tooltipProps from './tooltipProps';
export default defineComponent({
  name: 'x-tooltip',
  props: tooltipProps,
  slots: ['title'],
  emits: ['update:visible', 'visibleChange'],

  setup(props, {
    attrs,
    emit,
    slots
  }) {
    const {
      proxy: self
    } = getCurrentInstance();
    const {
      prefixCls,
      builtinPlacements
    } = props;
    const state = reactive({
      visible: props.visible
    });
    watch(() => props.visible, v => {
      state.visible = v;
    });

    function onPopupVisibleChange(v) {
      state.visible = v;
      emit('update:visible', v);
      emit('visibleChange', v);
    }

    function getPopupElement() {
      return getComponent(self, 'title');
    }

    return () => {
      var _slots$default;

      const popupClass = normalizeClass([`${prefixCls} ${prefixCls}-placement-${props.placement}`, props.popupClass]);
      const triggerProps = {
        action: props.trigger,
        disabled: props.disabled,
        popup: getPopupElement(),
        popupClass,
        popupTransitionName: props.transitionName || prefixCls,
        popupAlign: props.align || (builtinPlacements === null || builtinPlacements === void 0 ? void 0 : builtinPlacements[props.placement]) || placements[props.placement],
        getPopupContainer: props.getPopupContainer,
        onPopupVisibleChange,
        mouseEnterDelay: props.mouseEnterDelay,
        mouseLeaveDelay: props.mouseLeaveDelay,
        popupStyle: props.popupStyle,
        ...attrs
      };
      return _createVNode(Trigger, triggerProps, {
        default: () => [(_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)]
      });
    };
  }

});