/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _slicedToArray } from '../_chunks/dep-55846619.mjs';
import { _ as _defineProperty } from '../_chunks/dep-eb20e23f.mjs';
import { isVNode, defineComponent, toRefs, ref, inject, provide, computed, watch, onUnmounted, nextTick, withDirectives, createVNode, mergeProps, vShow, Transition } from 'vue';
import { createPopper } from '@popperjs/core';
import { i as isFunction_1 } from '../_chunks/dep-6b22dd27.mjs';
import { i as isObject_1 } from '../_chunks/dep-0f0c89ff.mjs';
import { i as isString_1 } from '../_chunks/dep-1cf2b129.mjs';
import '../hooks/index.mjs';
import { usePrefixClass, useCommonClassName } from '../hooks/useConfig.mjs';
import useVModel from '../hooks/useVModel.mjs';
import { on, off, once } from '../utils/dom.mjs';
import setStyle from '../_common/js/utils/set-style.mjs';
import Container from './container.mjs';
import PopupProps from './props.mjs';
import { useTNodeJSX, useContent } from '../hooks/tnode.mjs';
import '../_chunks/dep-cfcedf47.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../hooks/icon.mjs';
import '../hooks/slot.mjs';
import '../hooks/useCommonClassName.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../hooks/useDefaultValue.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../hooks/useDestroyOnClose.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useVirtualScroll.mjs';
import '../utils/easing.mjs';
import '../_chunks/dep-e091cfd9.mjs';
import '../hooks/useResizeObserver.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !isVNode(s);
}
var POPUP_ATTR_NAME = "data-td-popup";
var POPUP_PARENT_ATTR_NAME = "data-td-popup-parent";
function getPopperTree(id, upwards) {
  var list = [];
  var selectors = [POPUP_PARENT_ATTR_NAME, POPUP_ATTR_NAME];
  if (!id) return list;
  if (upwards) selectors.unshift(selectors.pop());
  recurse(id);
  return list;
  function recurse(id2) {
    var children = document.querySelectorAll("[".concat(selectors[0], "=\"").concat(id2, "\"]"));
    children.forEach(function (el) {
      list.push(el);
      var childId = el.getAttribute(selectors[1]);
      if (childId && childId !== id2) recurse(childId);
    });
  }
}
var parentKey = Symbol();
function getPopperPlacement(placement) {
  return placement.replace(/-(left|top)$/, "-start").replace(/-(right|bottom)$/, "-end");
}
function attachListeners(elm) {
  var offs = [];
  return {
    add: function add(type, listener) {
      if (!type) return;
      on(elm.value, type, listener);
      offs.push(function () {
        off(elm.value, type, listener);
      });
    },
    clean: function clean() {
      offs.forEach(function (handler) {
        return handler === null || handler === void 0 ? void 0 : handler();
      });
      offs.length = 0;
    }
  };
}
var _Popup = defineComponent({
  name: "TPopup",
  props: _objectSpread(_objectSpread({}, PopupProps), {}, {
    expandAnimation: {
      type: Boolean
    }
  }),
  setup: function setup(props2, _ref) {
    var _process$env;
    var expose = _ref.expose;
    var _toRefs = toRefs(props2),
      propVisible = _toRefs.visible,
      modelValue = _toRefs.modelValue;
    var _useVModel = useVModel(propVisible, modelValue, props2.defaultVisible, props2.onVisibleChange, "visible"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      visible = _useVModel2[0],
      setVisible = _useVModel2[1];
    var renderTNodeJSX = useTNodeJSX();
    var renderContent = useContent();
    var popper;
    var showTimeout;
    var hideTimeout;
    var triggerEl = ref(null);
    var overlayEl = ref(null);
    var popperEl = ref(null);
    var containerRef = ref(null);
    var id = typeof process !== "undefined" && (_process$env = process.env) !== null && _process$env !== void 0 && _process$env.TEST ? "" : Date.now().toString(36);
    var parent = inject(parentKey, void 0);
    provide(parentKey, {
      id: id,
      assertMouseLeave: onMouseLeave
    });
    var prefixCls = usePrefixClass("popup");
    var _useCommonClassName = useCommonClassName(),
      commonCls = _useCommonClassName.STATUS;
    var delay = computed(function () {
      var _props2$delay, _delay2$;
      var delay2 = props2.trigger !== "hover" ? [0, 0] : [].concat((_props2$delay = props2.delay) !== null && _props2$delay !== void 0 ? _props2$delay : [250, 150]);
      return {
        show: delay2[0],
        hide: (_delay2$ = delay2[1]) !== null && _delay2$ !== void 0 ? _delay2$ : delay2[0]
      };
    });
    var trigger = attachListeners(triggerEl);
    watch(function () {
      return [props2.trigger, triggerEl.value];
    }, function () {
      if (!triggerEl.value) return;
      trigger.clean();
      trigger.add({
        "hover": "mouseenter",
        "focus": "focusin",
        "context-menu": "contextmenu",
        "click": "click"
      }[props2.trigger], function (ev) {
        if (props2.disabled) return;
        if (ev.type === "contextmenu") ev.preventDefault();
        if ((ev.type === "click" || ev.type === "contextmenu") && visible.value) {
          hide(ev);
          return;
        }
        show(ev);
      });
      trigger.add({
        hover: "mouseleave",
        focus: "focusout"
      }[props2.trigger], hide);
    });
    watch(function () {
      return [props2.overlayStyle, props2.overlayInnerStyle, overlayEl.value];
    }, function () {
      updateOverlayInnerStyle();
      updatePopper();
    });
    watch(function () {
      return props2.placement;
    }, function () {
      destroyPopper();
      updatePopper();
    });
    watch(function () {
      return visible.value;
    }, function (visible2) {
      if (visible2) {
        on(document, "mousedown", onDocumentMouseDown, true);
        if (props2.trigger === "focus") {
          once(triggerEl.value, "keydown", function (ev) {
            var _process$env2;
            var code = typeof process !== "undefined" && (_process$env2 = process.env) !== null && _process$env2 !== void 0 && _process$env2.TEST ? "27" : "Escape";
            if (ev.code === code) hide(ev);
          });
        }
        return;
      }
      off(document, "mousedown", onDocumentMouseDown, true);
    });
    onUnmounted(function () {
      destroyPopper();
      clearAllTimeout();
      off(document, "mousedown", onDocumentMouseDown, true);
    });
    expose({
      update: updatePopper,
      close: hide,
      getOverlay: function getOverlay() {
        return overlayEl.value;
      }
    });
    function getOverlayStyle() {
      var overlayStyle = props2.overlayStyle;
      if (!triggerEl.value || !overlayEl.value) return;
      if (isFunction_1(overlayStyle)) return overlayStyle(triggerEl.value, overlayEl.value);
      if (isObject_1(overlayStyle)) return overlayStyle;
    }
    function updateOverlayInnerStyle() {
      var overlayInnerStyle = props2.overlayInnerStyle;
      if (!triggerEl.value || !overlayEl.value) return;
      if (isFunction_1(overlayInnerStyle)) setStyle(overlayEl.value, overlayInnerStyle(triggerEl.value, overlayEl.value));else if (isObject_1(overlayInnerStyle)) setStyle(overlayEl.value, overlayInnerStyle);
    }
    function updatePopper() {
      if (!popperEl.value || !visible.value) return;
      if (popper) {
        popper.state.elements.reference = triggerEl.value;
        popper.update();
        return;
      }
      popper = createPopper(triggerEl.value, popperEl.value, _objectSpread({
        placement: getPopperPlacement(props2.placement),
        onFirstUpdate: function onFirstUpdate() {
          nextTick(updatePopper);
        }
      }, props2.popperOptions));
    }
    function destroyPopper() {
      var _containerRef$value;
      if (popper) {
        var _popper;
        (_popper = popper) === null || _popper === void 0 ? void 0 : _popper.destroy();
        popper = null;
      }
      if (props2.destroyOnClose) (_containerRef$value = containerRef.value) === null || _containerRef$value === void 0 ? void 0 : _containerRef$value.unmountContent();
    }
    function show(ev) {
      clearAllTimeout();
      showTimeout = setTimeout(function () {
        setVisible(true, {
          trigger: getTriggerType(ev)
        });
      }, delay.value.show);
    }
    function hide(ev) {
      clearAllTimeout();
      hideTimeout = setTimeout(function () {
        setVisible(false, {
          trigger: getTriggerType(ev)
        });
      }, delay.value.hide);
    }
    function clearAllTimeout() {
      clearTimeout(showTimeout);
      clearTimeout(hideTimeout);
    }
    function getTriggerType(ev) {
      switch (ev.type) {
        case "mouseenter":
        case "mouseleave":
          return "trigger-element-hover";
        case "focusin":
          return "trigger-element-focus";
        case "focusout":
          return "trigger-element-blur";
        case "click":
          return "trigger-element-click";
        case "context-menu":
        case "keydown":
          return "keydown-esc";
        case "mousedown":
          return "document";
      }
    }
    function onDocumentMouseDown(ev) {
      if (popperEl.value.contains(ev.target)) return;
      if (triggerEl.value.contains(ev.target)) return;
      var activedPopper = getPopperTree(id).find(function (el) {
        return el.contains(ev.target);
      });
      if (activedPopper && getPopperTree(activedPopper.getAttribute(POPUP_PARENT_ATTR_NAME), true).includes(popperEl.value)) return;
      hide(ev);
    }
    function onMouseLeave(ev) {
      if (props2.trigger !== "hover" || triggerEl.value.contains(ev.target)) return;
      var isCursorOverlaps = getPopperTree(id).some(function (el) {
        var rect = el.getBoundingClientRect();
        return ev.x > rect.x && ev.x < rect.x + rect.width && ev.y > rect.y && ev.y < rect.y + rect.height;
      });
      if (!isCursorOverlaps) {
        hide(ev);
        parent === null || parent === void 0 ? void 0 : parent.assertMouseLeave(ev);
      }
    }
    var updateScrollTop = inject("updateScrollTop", void 0);
    function handleOnScroll(e) {
      var _props2$onScroll;
      var _e$target = e.target,
        scrollTop = _e$target.scrollTop,
        clientHeight = _e$target.clientHeight,
        scrollHeight = _e$target.scrollHeight;
      if (scrollHeight - scrollTop === clientHeight) {
        var _props2$onScrollToBot;
        (_props2$onScrollToBot = props2.onScrollToBottom) === null || _props2$onScrollToBot === void 0 ? void 0 : _props2$onScrollToBot.call(props2, {
          e: e
        });
      }
      (_props2$onScroll = props2.onScroll) === null || _props2$onScroll === void 0 ? void 0 : _props2$onScroll.call(props2, {
        e: e
      });
    }
    watch(function () {
      return [visible.value, overlayEl.value];
    }, function () {
      if (visible.value && overlayEl.value && updateScrollTop) updateScrollTop === null || updateScrollTop === void 0 ? void 0 : updateScrollTop(overlayEl.value);
    });
    return function () {
      var _ref2, _ref3;
      var content = renderTNodeJSX("content");
      var hidePopup = props2.hideEmptyPopup && ["", void 0, null].includes(content);
      var overlay = visible.value || !props2.destroyOnClose ? withDirectives(createVNode("div", mergeProps((_ref2 = {}, _defineProperty(_ref2, POPUP_ATTR_NAME, id), _defineProperty(_ref2, POPUP_PARENT_ATTR_NAME, parent === null || parent === void 0 ? void 0 : parent.id), _ref2), {
        "class": [prefixCls.value, props2.overlayClassName],
        "ref": function ref(ref2) {
          return popperEl.value = ref2;
        },
        "style": [{
          zIndex: props2.zIndex
        }, getOverlayStyle(), hidePopup && {
          visibility: "hidden"
        }]
      }, props2.trigger === "hover" && {
        onMouseenter: function onMouseenter() {
          if (visible.value) clearAllTimeout();
        },
        onMouseleave: onMouseLeave
      }), [createVNode("div", {
        "class": ["".concat(prefixCls.value, "__content"), (_ref3 = {}, _defineProperty(_ref3, "".concat(prefixCls.value, "__content--text"), isString_1(props2.content)), _defineProperty(_ref3, "".concat(prefixCls.value, "__content--arrow"), props2.showArrow), _defineProperty(_ref3, commonCls.value.disabled, props2.disabled), _ref3), props2.overlayInnerClassName],
        "ref": function ref(ref2) {
          return overlayEl.value = ref2;
        },
        "onScroll": handleOnScroll
      }, [content, props2.showArrow && createVNode("div", {
        "class": "".concat(prefixCls.value, "__arrow")
      }, null)])]), [[vShow, visible.value]]) : null;
      return createVNode(Container, {
        "ref": function ref(ref2) {
          return containerRef.value = ref2;
        },
        "forwardRef": function forwardRef(ref2) {
          return triggerEl.value = ref2;
        },
        "onContentMounted": function onContentMounted() {
          if (visible.value) {
            updatePopper();
            updateOverlayInnerStyle();
          }
        },
        "onResize": function onResize() {
          if (visible.value) updatePopper();
        },
        "visible": visible.value,
        "attach": props2.attach
      }, {
        content: function content() {
          return createVNode(Transition, {
            "name": "".concat(prefixCls.value, "--animation").concat(props2.expandAnimation ? "-expand" : ""),
            "appear": true,
            "onEnter": updatePopper,
            "onAfterLeave": destroyPopper
          }, _isSlot(overlay) ? overlay : {
            "default": function _default() {
              return [overlay];
            }
          });
        },
        "default": function _default() {
          return renderContent("default", "triggerElement");
        }
      });
    };
  }
});

export { _Popup as default };
//# sourceMappingURL=popup.mjs.map
