"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMutateObserver = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _vue = require("vue");
var _canUseDom = _interopRequireDefault(require("../_util/canUseDom"));
const defaultOptions = {
  subtree: true,
  childList: true,
  attributeFilter: ['style', 'class']
};
const useMutateObserver = (nodeList, callback, options = (0, _vue.ref)((0, _extends2.default)({}, defaultOptions))) => {
  (0, _vue.watch)([() => nodeList.value, () => options.value], ([rawNodeList, rawOptions], _, onCleanup) => {
    if (!(0, _canUseDom.default)() || !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'
  });
};
exports.useMutateObserver = useMutateObserver;