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

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { _ as _toConsumableArray } from '../_chunks/dep-bf1e33c8.mjs';
import { isVNode, Fragment, Text, ref, getCurrentInstance, onMounted, onUpdated, defineComponent, watch, createVNode, mergeProps, Teleport } from 'vue';
import { i as isUndefined_1 } from '../_chunks/dep-78847b2e.mjs';
import { i as isArray_1 } from '../_chunks/dep-b57180d0.mjs';
import useResizeObserver from '../hooks/useResizeObserver.mjs';
import { getSSRAttach, getAttach } from '../utils/dom.mjs';
import popupProps from './props.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../utils/easing.mjs';

function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !isVNode(s);
}
function filterEmpty() {
  var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var vnodes = [];
  children.forEach(function (child) {
    if (isArray_1(child)) vnodes.push.apply(vnodes, _toConsumableArray(child));else if (child.type === Fragment) vnodes.push.apply(vnodes, _toConsumableArray(filterEmpty(child.children)));else vnodes.push(child);
  });
  return vnodes.filter(function (c) {
    return !(c && (!isUndefined_1(Comment) && c.type === Comment || c.type === Fragment && c.children.length === 0 || c.type === Text && c.children.trim() === ""));
  });
}
function isContentRectChanged(rect1, rect2) {
  if (!rect1 && !rect2) return false;
  if (!rect1 || !rect2) return true;
  if (["width", "height", "x", "y"].some(function (k) {
    return rect1[k] !== rect2[k];
  })) return true;
  return false;
}
function useElement(elmFn, cb) {
  var el = ref(null);
  var instance = getCurrentInstance();
  if (!instance) {
    console.warn("useElement must be called in setup()");
    return;
  }
  onMounted(function () {
    el.value = elmFn(instance);
    cb === null || cb === void 0 ? void 0 : cb(el.value);
  });
  onUpdated(function () {
    var newEl = elmFn(instance);
    if (el.value !== newEl) {
      el.value = newEl;
      cb === null || cb === void 0 ? void 0 : cb(el.value);
    }
  });
  return el;
}
var Trigger = defineComponent({
  name: "TPopupTrigger",
  props: {
    forwardRef: Function
  },
  emits: ["resize"],
  setup: function setup(props2, _ref) {
    var emit = _ref.emit,
      slots = _ref.slots;
    var el = useElement(function (instance) {
      return instance.vnode.el;
    });
    var contentRect = ref(null);
    watch(el, function () {
      var _props2$forwardRef;
      (_props2$forwardRef = props2.forwardRef) === null || _props2$forwardRef === void 0 ? void 0 : _props2$forwardRef.call(props2, el.value);
    });
    useResizeObserver(el, function (_ref2) {
      var _ref3 = _slicedToArray(_ref2, 1),
        newContentRect = _ref3[0].contentRect;
      contentRect.value = newContentRect;
    });
    watch(contentRect, function (newRect, oldRect) {
      if (isContentRectChanged(newRect, oldRect)) emit("resize");
    });
    return function () {
      var _slots$default, _children$;
      var children = filterEmpty((_slots$default = slots["default"]) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots));
      if (children.length > 1 || ((_children$ = children[0]) === null || _children$ === void 0 ? void 0 : _children$.type) === Text) return createVNode("span", null, [children]);
      return children[0];
    };
  }
});
var Content = defineComponent({
  name: "TPopupContent",
  emits: ["resize"],
  setup: function setup(props2, _ref4) {
    var emit = _ref4.emit;
    var contentEl = useElement(function (instance) {
      return instance.vnode.el.children[0];
    });
    useResizeObserver(contentEl, function () {
      emit("resize");
    });
  },
  render: function render() {
    return createVNode("div", {
      "style": "position: absolute; top: 0px; left: 0px; width: 100%"
    }, [this.$slots["default"]()]);
  }
});
var Container = defineComponent({
  name: "TPopupContainer",
  inheritAttrs: false,
  props: {
    parent: Object,
    visible: Boolean,
    attach: popupProps.attach,
    forwardRef: Function
  },
  emits: ["resize", "contentMounted"],
  setup: function setup(props2, _ref5) {
    var emit = _ref5.emit;
    var triggerEl = ref(null);
    var mountContent = ref(false);
    onMounted(function () {
      requestAnimationFrame(function () {
        mountContent.value = props2.visible;
      });
    });
    watch(function () {
      return props2.visible;
    }, function (visible) {
      if (visible) mountContent.value = props2.visible;
    });
    return {
      mountContent: mountContent,
      triggerEl: triggerEl,
      unmountContent: function unmountContent() {
        mountContent.value = false;
      },
      emitResize: function emitResize() {
        emit("resize");
      },
      emitContentMounted: function emitContentMounted() {
        emit("contentMounted");
      }
    };
  },
  render: function render() {
    var _slot;
    var _this = this;
    var getElement = function getElement() {
      return getSSRAttach() || getAttach(_this.attach, _this.triggerEl);
    };
    return createVNode(Fragment, null, [createVNode(Trigger, mergeProps({
      "class": this.$attrs["class"]
    }, {
      "forwardRef": function forwardRef(el) {
        _this.forwardRef(el);
        _this.triggerEl = el;
      },
      "onResize": this.emitResize
    }), _isSlot(_slot = this.$slots["default"]()) ? _slot : {
      "default": function _default() {
        return [_slot];
      }
    }), this.mountContent && createVNode(Teleport, {
      "disabled": !getElement(),
      "to": getElement()
    }, {
      "default": function _default() {
        return [createVNode(Content, {
          "onResize": _this.emitResize,
          "onVnodeMounted": _this.emitContentMounted
        }, {
          "default": function _default() {
            return [_this.$slots.content && _this.$slots.content()];
          }
        })];
      }
    })]);
  }
});

export { Container as default };
//# sourceMappingURL=container.mjs.map
