import { watch } from 'vue';
import { tryOnScopeDispose } from '../try-on-scope-dispose/index.mjs';
import { unrefElement } from '../unref-element/index.mjs';
import { useSupported } from '../use-supported/index.mjs';
import { defaultWindow } from '../_configurable.mjs';

function useMutationObserver(target, callback, options = {}) {
  const { window = defaultWindow, ...mutationOptions } = options;
  let observer;
  const isSupported = useSupported(() => window && "MutationObserver" in window);
  const cleanup = () => {
    if (observer) {
      observer.disconnect();
      observer = void 0;
    }
  };
  const stopWatch = watch(
    () => unrefElement(target),
    (el) => {
      cleanup();
      if (isSupported.value && window && el) {
        observer = new MutationObserver(callback);
        observer.observe(el, mutationOptions);
      }
    },
    { immediate: true }
  );
  const stop = () => {
    cleanup();
    stopWatch();
  };
  tryOnScopeDispose(stop);
  return {
    isSupported,
    stop
  };
}

export { useMutationObserver };
//# sourceMappingURL=index.mjs.map
