"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMutationObserver = exports.EuiMutationObserver = void 0;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _use_observer = require("../use_observer");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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) { _defineProperty(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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
 * 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.propTypes = {
  /**
     * ReactNode to render as this component's content
     */
  children: _propTypes.default.func.isRequired,
  onMutation: _propTypes.default.any.isRequired,
  observerOptions: _propTypes.default.any
};
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]);
};