import _extends from "@babel/runtime/helpers/esm/extends";
import { ref, watch } from 'vue';
import canUseDom from '../_util/canUseDom';
const defaultOptions = {
  subtree: true,
  childList: true,
  attributeFilter: ['style', 'class']
};
export const useMutateObserver = (nodeList, callback, options = ref(_extends({}, defaultOptions))) => {
  watch([() => nodeList.value, () => options.value], ([rawNodeList, rawOptions], _, onCleanup) => {
    if (!canUseDom() || !rawNodeList) {
      return;
    }
    let instance;
    const nodeList = Array.isArray(rawNodeList) ? rawNodeList : [rawNodeList];
    if ('MutationObserver' in window) {
      instance = new MutationObserver(callback);
      nodeList.forEach(node => {
        instance.observe(node, rawOptions);
      });
    }
    onCleanup(() => {
      instance === null || instance === void 0 ? void 0 : instance.takeRecords();
      instance === null || instance === void 0 ? void 0 : instance.disconnect();
    });
  }, {
    immediate: true,
    flush: 'post'
  });
};