import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode } from "vue";
// @ts-nocheck
import { normalizeClass } from "@vue/shared";
import { defineComponent, computed, Transition } from "vue";
import Align from '../align';
const popupProps = {
  prefixCls: String,
  visible: Boolean,
  getClassNameFromAlign: Function,
  getRootDomNode: Function,
  align: Object,
  destroyPopupOnHide: Boolean,
  getContainer: Function,
  transitionName: String,
  animation: Object,
  mask: Boolean,
  maskTransitionName: String,
  maskAnimation: Object,
  zIndex: Number,
  popupClass: [String, Object, Array],
  popupStyle: Object,
  stretch: String,
  point: Object
};
export default defineComponent({
  name: 'TriggerPopup',
  props: popupProps,
  inheritAttrs: false,

  setup(props, {
    attrs,
    slots
  }) {
    const target = computed(() => props.point || props.getRootDomNode);

    function onAlign(popupDomNode, align) {
      var _attrs$onAlign;

      // todo
      // popupDomNode.className = 
      (_attrs$onAlign = attrs.onAlign) === null || _attrs$onAlign === void 0 ? void 0 : _attrs$onAlign.call(attrs, ...arguments);
    }

    function getZIndexStyle() {
      const style = {};

      if (props.zIndex !== undefined) {
        style.zIndex = props.zIndex;
      }

      return style;
    }

    function getPopupElement() {
      var _slots$default;

      const popupInnerProps = {
        style: { ...attrs.style,
          ...props.popupStyle,
          ...getZIndexStyle()
        },
        class: normalizeClass([attrs.class, props.prefixCls, props.popupClass]),
        ...attrs
      };
      const t = props.transitionName || props.prefixCls;
      /**
        todo
        但使用 v-show 时，下面代码会有bug
          visible = false
          nextTick(() => visible = true)
            暂时用 v-if 解决
       */

      return _createVNode(Transition, {
        "name": t
      }, {
        default: () => [props.visible && _createVNode(Align, {
          "target": target.value,
          "align": props.align,
          "onAlign": onAlign
        }, {
          default: () => [_createVNode("div", popupInnerProps, [(_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)])]
        })]
      });
    }

    function getMaskElement() {
      if (props.mask) {
        const name = props.maskTransitionName || `${props.prefixCls}-mask`;
        return _createVNode(Transition, {
          "appear": true,
          "name": name
        }, {
          default: () => [_withDirectives(_createVNode("div", {
            "class": `${props.prefixCls}-mask`,
            "style": getZIndexStyle()
          }, null), [[_vShow, props.visible]])]
        });
      }
    }

    return () => _createVNode("div", {
      "style": "position:absolute; top:0; left:0; width:100%"
    }, [getMaskElement(), getPopupElement()]);
  }

});