'use strict';

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

var ResizeObserver$1 = require('resize-observer-polyfill');
var vue = require('vue');
require('../../../utils/index.cjs');
var props = require('../../../utils/vue/props.cjs');

var ResizeObserver = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ResizeObserver",
  props: {
    disabled: Boolean,
    onResize: Function
  },
  emits: ["resize"],
  setup(props$1, {
    slots
  }) {
    const state = vue.reactive({
      width: 0,
      height: 0,
      offsetHeight: 0,
      offsetWidth: 0
    });
    let currentElement = null;
    let resizeObserver = null;
    const destroyObserver = () => {
      if (resizeObserver) {
        resizeObserver.disconnect();
        resizeObserver = null;
      }
    };
    const onResize = (entries) => {
      const {
        onResize: onResize2
      } = props$1;
      const target = entries[0].target;
      const {
        width,
        height
      } = target.getBoundingClientRect();
      const {
        offsetWidth,
        offsetHeight
      } = target;
      const fixedWidth = Math.floor(width);
      const fixedHeight = Math.floor(height);
      if (state.width !== fixedWidth || state.height !== fixedHeight || state.offsetWidth !== offsetWidth || state.offsetHeight !== offsetHeight) {
        const size = {
          width: fixedWidth,
          height: fixedHeight,
          offsetWidth,
          offsetHeight
        };
        Object.assign(state, size);
        if (onResize2) {
          Promise.resolve().then(() => {
            onResize2({
              ...size,
              offsetWidth,
              offsetHeight
            }, target);
          });
        }
      }
    };
    const instance = vue.getCurrentInstance();
    const registerObserver = () => {
      const {
        disabled
      } = props$1;
      if (disabled) {
        destroyObserver();
        return;
      }
      const element = props.findDOMNode(instance);
      const elementChanged = element !== currentElement;
      if (elementChanged) {
        destroyObserver();
        currentElement = element;
      }
      if (!resizeObserver && element) {
        resizeObserver = new ResizeObserver$1(onResize);
        resizeObserver.observe(element);
      }
    };
    vue.onMounted(() => {
      registerObserver();
    });
    vue.onUpdated(() => {
      registerObserver();
    });
    vue.onUnmounted(() => {
      destroyObserver();
    });
    vue.watch(() => props$1.disabled, () => {
      registerObserver();
    }, {
      flush: "post"
    });
    return () => {
      var _a;
      return (_a = slots.default) == null ? void 0 : _a.call(slots)[0];
    };
  }
});

exports.default = ResizeObserver;
//# sourceMappingURL=index.cjs.map
