"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMutationObserver = exports.EuiMutationObserver = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = require("react");
var _use_observer = require("../use_observer");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */
var EuiMutationObserver = exports.EuiMutationObserver = function EuiMutationObserver(_ref) {
  var children = _ref.children,
    onMutation = _ref.onMutation,
    observerOptions = _ref.observerOptions;
  // Store onMutation and observerOptions in refs so the observer callback
  // and setup always use the latest prop values without needing to
  // re-subscribe (which would cause the ref callback to cycle)
  var onMutationRef = (0, _react.useRef)(onMutation);
  onMutationRef.current = onMutation;
  var observerOptionsRef = (0, _react.useRef)(observerOptions);
  observerOptionsRef.current = observerOptions;
  var mutationCallback = (0, _react.useCallback)(function (records, observer) {
    onMutationRef.current(records, observer);
  }, []);
  var beginObserve = (0, _react.useCallback)(function (node) {
    return makeMutationObserver(node, observerOptionsRef.current, mutationCallback);
  }, [mutationCallback]);
  var updateChildNode = (0, _use_observer.useObserver)(beginObserve, 'EuiMutationObserver');
  return children(updateChildNode);
};
EuiMutationObserver.displayName = 'EuiMutationObserver';
var makeMutationObserver = function makeMutationObserver(node, _observerOptions, callback) {
  // The MutationObserver polyfill used in Kibana (for Jest) implements
  // an older spec in which specifying `attributeOldValue` or `attributeFilter`
  // without specifying `attributes` results in a `SyntaxError`.
  // The following logic patches the newer spec in which `attributes: true` can be
  // implied when appropriate (`attributeOldValue` or `attributeFilter` is specified).
  var observerOptions = _objectSpread({}, _observerOptions);
  var needsAttributes = observerOptions.hasOwnProperty('attributeOldValue') || observerOptions.hasOwnProperty('attributeFilter');
  if (needsAttributes && !observerOptions.hasOwnProperty('attributes')) {
    observerOptions.attributes = true;
  }
  var observer = new MutationObserver(callback);
  observer.observe(node, observerOptions);
  return observer;
};
var useMutationObserver = exports.useMutationObserver = function useMutationObserver(container, callback, observerOptions) {
  (0, _react.useEffect)(function () {
    if (container != null) {
      var observer = makeMutationObserver(container, observerOptions, callback);
      return function () {
        return observer.disconnect();
      };
    }
  },
  // ignore changing observerOptions
  // eslint-disable-next-line
  [container, callback]);
};