/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { removeDom, getAttach } from '../utils/dom.js';
import popupProps from './props.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';

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 isContentRectChanged(rect1, rect2) {
  if (!rect1 || !rect2) return;

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

  return false;
}

var Ref = Vue.extend({
  data: function data() {
    return {
      contentRect: null
    };
  },
  mounted: function mounted() {
    var _window;

    if ((_window = window) !== null && _window !== void 0 && _window.ResizeObserver && this.$el) {
      var el = this.$el;
      var vm = this;
      var ro = new ResizeObserver(function () {
        var entries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

        var _ref = entries[0] || {},
            contentRect = _ref.contentRect;

        if (isContentRectChanged(contentRect, vm.contentRect)) {
          vm.contentRect = contentRect;
          vm.$emit("resize", _objectSpread({}, contentRect));
          return;
        }

        if (!vm.contentRect) {
          vm.contentRect = contentRect;
        }
      });
      ro.observe(el);
      this.$on("hook:destroyed", function () {
        ro.unobserve(el);
      });
    }
  },
  render: function render() {
    var _children$;

    var h = arguments[0];
    var 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 Container = Vue.extend({
  props: {
    parent: Object,
    visible: Boolean,
    attach: popupProps.attach
  },
  data: function data() {
    return {
      content: null
    };
  },
  mounted: function mounted() {
    var _this = this;

    if (this.visible) {
      this.mountContent();
    }

    this.$watch("visible", function (visible) {
      if (visible) {
        _this.mountContent();
      }
    });
  },
  destroyed: function destroyed() {
    this.unmountContent();
  },
  methods: {
    mountContent: function mountContent() {
      var h = this.$createElement;
      if (this.content) return;
      var parent = this;
      var 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: parent,
        render: function render() {
          var h = arguments[0];
          return h("div", [parent.$slots.content]);
        },
        mounted: function mounted() {
          parent.$emit("mounted");
        },
        destroyed: function destroyed() {
          parent.content = null;
          removeDom(elm);
        }
      });
      getAttach(this.attach).appendChild(elm);
      this.content.$mount(elm.children[0]);
    },
    unmountContent: function unmountContent() {
      var _this$content;

      (_this$content = this.content) === null || _this$content === void 0 ? void 0 : _this$content.$destroy();
    },
    updateContent: function updateContent() {
      var _this$content2;

      (_this$content2 = this.content) === null || _this$content2 === void 0 ? void 0 : _this$content2.$forceUpdate();
    }
  },
  render: function render() {
    var _this2 = this;

    var h = arguments[0];
    return h(Ref, {
      "on": {
        "resize": function resize(ev) {
          return _this2.$emit("refResize", ev);
        }
      }
    }, [this.$slots["default"]]);
  }
});

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