/**
 * ldesign v0.2.3
 * (c) 2022年9月24日 swimly
 * @license MIT
 */

'use strict';

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

var Vue = require('vue');
var raf = require('raf');
var utils_dom = require('../../utils/dom.js');
var components_popup_props = require('./props.js');
require('lodash/isString');
require('../../utils/easing.js');

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

var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var raf__default = /*#__PURE__*/_interopDefaultLegacy(raf);

function isContentRectChanged(rect1, rect2) {
  if (!rect1 || !rect2) return;

  if (["width", "height", "x", "y"].some(k => rect1[k] !== rect2[k])) {
    return true;
  }

  return false;
}

function observeResize(elm, cb) {
  var _window;

  if (!((_window = window) !== null && _window !== void 0 && _window.ResizeObserver) || !elm) return;
  let prevContentRect = null;
  const ro = new ResizeObserver(function () {
    let entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    const {
      contentRect
    } = entries[0] || {};

    if (isContentRectChanged(contentRect, prevContentRect)) {
      prevContentRect = contentRect;
      cb(contentRect);
      return;
    }

    if (!prevContentRect) {
      prevContentRect = contentRect;
    }
  });
  ro.observe(elm);
  return function () {
    ro.unobserve(elm);
  };
}

const LTrigger = Vue__default["default"].extend({
  data() {
    return {
      contentRect: null
    };
  },

  mounted() {
    this.$on("hook:destroyed", observeResize(this.$el, ev => {
      this.$emit("resize", ev);
    }));
  },

  render() {
    var _children$;

    const h = arguments[0];
    const children = this.$slots.default || [];

    if (children.length > 1 || !((_children$ = children[0]) !== null && _children$ !== void 0 && _children$.tag)) {
      return h("span", [children]);
    }

    return children[0];
  }

});
var LContainer = Vue__default["default"].extend({
  props: {
    parent: Object,
    visible: Boolean,
    attach: components_popup_props["default"].attach
  },
  components: {
    LTrigger
  },

  data() {
    return {
      content: null
    };
  },

  mounted() {
    if (this.visible) {
      raf__default["default"](this.mountContent);
    }

    this.$watch("visible", visible => {
      if (visible) {
        this.mountContent();
      }
    });
  },

  destroyed() {
    this.unmountContent();
  },

  methods: {
    mountContent() {
      var _this$$refs, _this$$refs$triggerRe;

      this.$createElement;
      if (this.content) return;
      const parent = this;
      const elm = document.createElement("div");
      elm.style.cssText = "position: absolute; top: 0px; left: 0px; width: 100%";
      elm.appendChild(document.createElement("div"));
      this.content = new this.$root.constructor({
        parent,

        render() {
          const h = arguments[0];
          return h("div", [parent.$slots.content]);
        },

        mounted() {
          parent.$emit("contentMounted");
          const content = this.$el.children[0];

          if (content) {
            this.$on("hook:destroyed", observeResize(content, () => {
              parent.$emit("resize");
            }));
          }
        },

        destroyed() {
          parent.content = null;
          utils_dom.removeDom(elm);
        }

      });
      utils_dom.getAttach(this.attach, (_this$$refs = this.$refs) === null || _this$$refs === void 0 ? void 0 : (_this$$refs$triggerRe = _this$$refs.triggerRef) === null || _this$$refs$triggerRe === void 0 ? void 0 : _this$$refs$triggerRe.$el).appendChild(elm);
      this.content.$mount(elm.children[0]);
    },

    unmountContent() {
      var _this$content;

      (_this$content = this.content) === null || _this$content === void 0 ? void 0 : _this$content.$destroy();
    },

    updateContent() {
      var _this$content2;

      (_this$content2 = this.content) === null || _this$content2 === void 0 ? void 0 : _this$content2.$forceUpdate();
    }

  },

  render() {
    const h = arguments[0];
    return h("l-trigger", {
      "ref": "triggerRef",
      "on": {
        "resize": () => this.$emit("resize")
      }
    }, [this.$slots.default]);
  }

});

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