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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var vue = require('vue');
var isUndefined = require('lodash/isUndefined');
var isArray = require('lodash/isArray');
var hooks_useResizeObserver = require('../hooks/useResizeObserver.js');
var utils_dom = require('../utils/dom.js');
var popup_props = require('./props.js');
require('../_chunks/dep-f4db2590.js');
require('lodash/isString');
require('lodash/isFunction');
require('../utils/easing.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var isUndefined__default = /*#__PURE__*/_interopDefaultLegacy(isUndefined);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !vue.isVNode(s);
}
function filterEmpty() {
  var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var vnodes = [];
  children.forEach(function (child) {
    if (isArray__default["default"](child)) vnodes.push.apply(vnodes, _toConsumableArray__default["default"](child));else if (child.type === vue.Fragment) vnodes.push.apply(vnodes, _toConsumableArray__default["default"](filterEmpty(child.children)));else vnodes.push(child);
  });
  return vnodes.filter(function (c) {
    return !(c && (!isUndefined__default["default"](Comment) && c.type === Comment || c.type === vue.Fragment && c.children.length === 0 || c.type === vue.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 = vue.ref(null);
  var instance = vue.getCurrentInstance();
  if (!instance) {
    console.warn("useElement must be called in setup()");
    return;
  }
  vue.onMounted(function () {
    el.value = elmFn(instance);
    cb === null || cb === void 0 ? void 0 : cb(el.value);
  });
  vue.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 = vue.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 = vue.ref(null);
    vue.watch(el, function () {
      var _props2$forwardRef;
      (_props2$forwardRef = props2.forwardRef) === null || _props2$forwardRef === void 0 ? void 0 : _props2$forwardRef.call(props2, el.value);
    });
    hooks_useResizeObserver["default"](el, function (_ref2) {
      var _ref3 = _slicedToArray__default["default"](_ref2, 1),
        newContentRect = _ref3[0].contentRect;
      contentRect.value = newContentRect;
    });
    vue.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) === vue.Text) return vue.createVNode("span", null, [children]);
      return children[0];
    };
  }
});
var Content = vue.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];
    });
    hooks_useResizeObserver["default"](contentEl, function () {
      emit("resize");
    });
  },
  render: function render() {
    return vue.createVNode("div", {
      "style": "position: absolute; top: 0px; left: 0px; width: 100%"
    }, [this.$slots["default"]()]);
  }
});
var Container = vue.defineComponent({
  name: "TPopupContainer",
  inheritAttrs: false,
  props: {
    parent: Object,
    visible: Boolean,
    attach: popup_props["default"].attach,
    forwardRef: Function
  },
  emits: ["resize", "contentMounted"],
  setup: function setup(props2, _ref5) {
    var emit = _ref5.emit;
    var triggerEl = vue.ref(null);
    var mountContent = vue.ref(false);
    vue.onMounted(function () {
      requestAnimationFrame(function () {
        mountContent.value = props2.visible;
      });
    });
    vue.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 utils_dom.getSSRAttach() || utils_dom.getAttach(_this.attach, _this.triggerEl);
    };
    return vue.createVNode(vue.Fragment, null, [vue.createVNode(Trigger, vue.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 && vue.createVNode(vue.Teleport, {
      "disabled": !getElement(),
      "to": getElement()
    }, {
      "default": function _default() {
        return [vue.createVNode(Content, {
          "onResize": _this.emitResize,
          "onVnodeMounted": _this.emitContentMounted
        }, {
          "default": function _default() {
            return [_this.$slots.content && _this.$slots.content()];
          }
        })];
      }
    })]);
  }
});

exports["default"] = Container;
//# sourceMappingURL=container.js.map
