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

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { isVNode, defineComponent, toRefs, ref, inject, computed, nextTick, watch, onUnmounted, provide, withDirectives, createVNode, mergeProps, vShow, Transition } from 'vue';
import { createPopper } from '@popperjs/core';
import { i as isString_1 } from '../_chunks/dep-876fb3ec.mjs';
import { i as isFunction_1 } from '../_chunks/dep-5717e263.mjs';
import { i as isObject_1 } from '../_chunks/dep-a8bcacea.mjs';
import { usePrefixClass, useCommonClassName } from '../hooks/useConfig.mjs';
import { on, off, once } from '../utils/dom.mjs';
import { renderTNodeJSX, renderContent } from '../utils/render-tnode.mjs';
import setStyle from '../_common/js/utils/set-style.mjs';
import useVModel from '../hooks/useVModel.mjs';
import popupProps from './props.mjs';
import Container from './container.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../utils/easing.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-78847b2e.mjs';
import '../hooks/useResizeObserver.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 triggers = ["click", "hover", "focus", "context-menu"];
var injectionKey = Symbol("popup");
function getPopperPlacement(placement) {
  return placement.replace(/-(left|top)$/, "-start").replace(/-(right|bottom)$/, "-end");
}
function attachListeners(elm) {
  var offs = [];
  return {
    add: function add(type, listener) {
      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 expose = _ref.expose;
    var _toRefs = toRefs(props2),
      visible = _toRefs.visible,
      modelValue = _toRefs.modelValue;
    var _useVModel = useVModel(visible, modelValue, props2.defaultVisible, props2.onVisibleChange, "visible"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      innerVisible = _useVModel2[0],
      setInnerVisible = _useVModel2[1];
    var popper;
    var timeoutId;
    var hasDocumentEvent = false;
    var visibleState = ref(0);
    var mouseInRange = ref(false);
    var contentClicked = ref(false);
    var triggerEl = ref(null);
    var overlayEl = ref(null);
    var popperEl = ref(null);
    var containerRef = ref(null);
    var parent = inject(injectionKey, void 0);
    var prefixCls = usePrefixClass("popup");
    var _useCommonClassName = useCommonClassName(),
      commonCls = _useCommonClassName.STATUS;
    var overlayCls = computed(function () {
      var _ref2;
      return ["".concat(prefixCls.value, "__content"), (_ref2 = {}, _defineProperty(_ref2, "".concat(prefixCls.value, "__content--text"), isString_1(props2.content)), _defineProperty(_ref2, "".concat(prefixCls.value, "__content--arrow"), props2.showArrow), _defineProperty(_ref2, commonCls.value.disabled, props2.disabled), _ref2), props2.overlayInnerClassName];
    });
    var hasTrigger = computed(function () {
      return triggers.reduce(function (map, trigger2) {
        return _objectSpread(_objectSpread({}, map), {}, _defineProperty({}, trigger2, props2.trigger.includes(trigger2)));
      }, {});
    });
    var delay = computed(function () {
      var _props2$delay, _delay2$;
      var delay2 = [].concat((_props2$delay = props2.delay) !== null && _props2$delay !== void 0 ? _props2$delay : [250, 150]);
      return {
        open: delay2[0],
        close: (_delay2$ = delay2[1]) !== null && _delay2$ !== void 0 ? _delay2$ : delay2[0]
      };
    });
    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 || !innerVisible.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 emitVisible(visible2, context) {
      if (props2.disabled || visible2 === innerVisible.value) return;
      if (!visible2 && visibleState.value > 1) return;
      setInnerVisible(visible2, context);
    }
    function preventClosing(preventing) {
      parent === null || parent === void 0 ? void 0 : parent.preventClosing(preventing);
      if (preventing) {
        visibleState.value += 1;
      } else if (visibleState.value) {
        visibleState.value -= 1;
        if (!visibleState.value) {
          emitVisible(false, {});
          if (parent !== null && parent !== void 0 && parent.hasTrigger.value.hover && !(parent !== null && parent !== void 0 && parent.mouseInRange)) parent.emitVisible(false, {});
        }
      }
    }
    function handleToggle(context) {
      emitVisible(!innerVisible.value, context);
    }
    function handleOpen(context) {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(function () {
        emitVisible(true, context);
      }, hasTrigger.value.click ? 0 : delay.value.open);
    }
    function handleClose(context) {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(function () {
        emitVisible(false, context);
      }, hasTrigger.value.click ? 0 : delay.value.close);
    }
    function handleDocumentClick(ev) {
      if (contentClicked.value) {
        setTimeout(function () {
          contentClicked.value = false;
        });
        return;
      }
      if (triggerEl.value.contains(ev.target)) return;
      visibleState.value = 0;
      emitVisible(false, {
        trigger: "document"
      });
    }
    function onMouseEnter() {
      mouseInRange.value = true;
      handleOpen({});
    }
    function onMouseLeave(ev) {
      if (visibleState.value > 1) {
        var rect = popperEl.value.getBoundingClientRect();
        if (ev.x > rect.x && ev.x < rect.x + rect.width && ev.y > rect.y && ev.y < rect.y + rect.height) return;
      }
      mouseInRange.value = false;
      handleClose({});
      if (parent !== null && parent !== void 0 && parent.mouseInRange) parent.onMouseLeave(ev);
    }
    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
      });
    }
    var trigger = attachListeners(triggerEl);
    watch(function () {
      return [props2.trigger, triggerEl.value];
    }, function () {
      if (!triggerEl.value) return;
      trigger.clean();
      if (hasTrigger.value.hover) {
        trigger.add("mouseenter", function () {
          return handleOpen({
            trigger: "trigger-element-hover"
          });
        });
        trigger.add("mouseleave", function () {
          return handleClose({
            trigger: "trigger-element-hover"
          });
        });
      } else if (hasTrigger.value.focus) {
        trigger.add("focusin", function () {
          return handleOpen({
            trigger: "trigger-element-focus"
          });
        });
        trigger.add("focusout", function () {
          return handleClose({
            trigger: "trigger-element-blur"
          });
        });
      } else if (hasTrigger.value.click) {
        trigger.add("click", function (e) {
          handleToggle({
            e: e,
            trigger: "trigger-element-click"
          });
        });
      } else if (hasTrigger.value["context-menu"]) {
        trigger.add("contextmenu", function (e) {
          e.preventDefault();
          e.button === 2 && handleToggle({
            trigger: "context-menu"
          });
        });
      }
    });
    watch(function () {
      return [props2.overlayStyle, props2.overlayInnerStyle, overlayEl.value];
    }, function () {
      updateOverlayInnerStyle();
      updatePopper();
    });
    var updateScrollTop = inject("updateScrollTop", function () {});
    watch(function () {
      return [innerVisible.value, overlayEl.value];
    }, function () {
      if (innerVisible.value && overlayEl.value && updateScrollTop) updateScrollTop === null || updateScrollTop === void 0 ? void 0 : updateScrollTop(overlayEl.value);
    });
    watch(function () {
      return props2.placement;
    }, function () {
      destroyPopper();
      updatePopper();
    });
    watch(contentClicked, function (clicked) {
      if (parent) parent.contentClicked.value = clicked;
    });
    watch(function () {
      return innerVisible.value;
    }, function (visible2) {
      if (visible2) {
        preventClosing(true);
        if (!hasDocumentEvent) {
          on(document, "click", handleDocumentClick, true);
          hasDocumentEvent = true;
        }
        if (triggerEl.value && hasTrigger.value.focus) {
          once(triggerEl.value, "keydown", function (ev) {
            if (ev.code === "Escape") handleClose({
              trigger: "keydown-esc"
            });
          });
        }
      } else {
        preventClosing(false);
        off(document, "click", handleDocumentClick, true);
        hasDocumentEvent = false;
        mouseInRange.value = false;
      }
    });
    onUnmounted(function () {
      if (innerVisible.value) parent === null || parent === void 0 ? void 0 : parent.preventClosing(false);
      destroyPopper();
      off(document, "click", handleDocumentClick, true);
      clearTimeout(timeoutId);
    });
    provide(injectionKey, {
      preventClosing: preventClosing,
      emitVisible: emitVisible,
      contentClicked: contentClicked,
      mouseInRange: mouseInRange,
      onMouseLeave: onMouseLeave,
      hasTrigger: hasTrigger
    });
    expose({
      handleClose: handleClose,
      updatePopper: updatePopper,
      getOverlay: function getOverlay() {
        return overlayEl.value;
      }
    });
    return {
      containerRef: containerRef,
      innerVisible: innerVisible,
      triggerEl: triggerEl,
      overlayEl: overlayEl,
      popperEl: popperEl,
      prefixCls: prefixCls,
      overlayCls: overlayCls,
      hasTrigger: hasTrigger,
      contentClicked: contentClicked,
      updatePopper: updatePopper,
      destroyPopper: destroyPopper,
      getOverlayStyle: getOverlayStyle,
      updateOverlayInnerStyle: updateOverlayInnerStyle,
      emitVisible: emitVisible,
      onMouseEnter: onMouseEnter,
      onMouseLeave: onMouseLeave,
      handleOnScroll: handleOnScroll
    };
  },
  render: function render() {
    var _this = this;
    var prefixCls = this.prefixCls,
      innerVisible = this.innerVisible,
      destroyOnClose = this.destroyOnClose,
      hasTrigger = this.hasTrigger,
      getOverlayStyle = this.getOverlayStyle,
      handleOnScroll = this.handleOnScroll;
    var content = renderTNodeJSX(this, "content");
    var hidePopup = this.hideEmptyPopup && ["", void 0, null].includes(content);
    var overlay = innerVisible || !destroyOnClose ? withDirectives(createVNode("div", mergeProps({
      "class": [prefixCls, this.overlayClassName],
      "ref": "popperEl",
      "style": [hidePopup && {
        visibility: "hidden",
        pointerEvents: "none"
      }, {
        zIndex: this.zIndex
      }, getOverlayStyle()],
      "onMousedown": function onMousedown() {
        _this.contentClicked = true;
      }
    }, hasTrigger.hover && {
      onMouseenter: this.onMouseEnter,
      onMouseleave: this.onMouseLeave
    }), [createVNode("div", {
      "class": this.overlayCls,
      "ref": "overlayEl",
      "onScroll": handleOnScroll
    }, [content, this.showArrow && createVNode("div", {
      "class": "".concat(prefixCls, "__arrow")
    }, null)])]), [[vShow, innerVisible]]) : null;
    return createVNode(Container, {
      "ref": "containerRef",
      "forwardRef": function forwardRef(ref2) {
        return _this.triggerEl = ref2;
      },
      "onContentMounted": function onContentMounted() {
        if (innerVisible) {
          _this.updatePopper();
          _this.updateOverlayInnerStyle();
        }
      },
      "onResize": function onResize() {
        if (innerVisible) _this.updatePopper();
      },
      "visible": innerVisible,
      "attach": this.attach
    }, {
      content: function content() {
        return createVNode(Transition, {
          "name": _this.expandAnimation ? "".concat(prefixCls, "--animation-expand") : "".concat(prefixCls, "--animation"),
          "appear": true,
          "onEnter": _this.updatePopper,
          "onAfterLeave": _this.destroyPopper
        }, _isSlot(overlay) ? overlay : {
          "default": function _default() {
            return [overlay];
          }
        });
      },
      "default": function _default() {
        return renderContent(_this, "default", "triggerElement");
      }
    });
  }
});

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