function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
/*
 * 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.
 */

import { useCallback, useEffect, useRef, useState } from 'react';
import PropTypes from "prop-types";
import { useObserver } from '../use_observer';
export var hasResizeObserver = typeof window !== 'undefined' && typeof window.ResizeObserver !== 'undefined';
export var EuiResizeObserver = function EuiResizeObserver(_ref) {
  var children = _ref.children,
    onResize = _ref.onResize;
  var onResizeRef = useRef(onResize);
  onResizeRef.current = onResize;
  var sizeRef = useRef({
    height: 0,
    width: 0
  });
  var resizeCallback = useCallback(function (_ref2) {
    var _ref3 = _slicedToArray(_ref2, 1),
      entry = _ref3[0];
    var _entry$borderBoxSize$ = entry.borderBoxSize[0],
      width = _entry$borderBoxSize$.inlineSize,
      height = _entry$borderBoxSize$.blockSize;

    // Check for actual resize event
    if (sizeRef.current.height === height && sizeRef.current.width === width) {
      return;
    }
    sizeRef.current = {
      height: height,
      width: width
    };
    onResizeRef.current({
      height: height,
      width: width
    });
  }, []);
  var beginObserve = useCallback(function (node) {
    return makeResizeObserver(node, resizeCallback);
  }, [resizeCallback]);
  var updateChildNode = useObserver(beginObserve, 'EuiResizeObserver');
  return children(updateChildNode);
};
EuiResizeObserver.propTypes = {
  /**
     * ReactNode to render as this component's content
     */
  children: PropTypes.func.isRequired,
  onResize: PropTypes.func.isRequired
};
EuiResizeObserver.displayName = 'EuiResizeObserver';
var makeResizeObserver = function makeResizeObserver(node, callback) {
  var observer;
  if (hasResizeObserver) {
    observer = new window.ResizeObserver(callback);
    observer.observe(node);
  }
  return observer;
};
export var useResizeObserver = function useResizeObserver(container, dimension) {
  var _useState = useState({
      width: 0,
      height: 0
    }),
    _useState2 = _slicedToArray(_useState, 2),
    size = _useState2[0],
    _setSize = _useState2[1];

  // _currentDimensions and _setSize are used to only store the
  // new state (and trigger a re-render) when the new dimensions actually differ
  var _currentDimensions = useRef(size);
  var setSize = useCallback(function (dimensions) {
    var doesWidthMatter = dimension !== 'height';
    var doesHeightMatter = dimension !== 'width';
    if (doesWidthMatter && _currentDimensions.current.width !== dimensions.width || doesHeightMatter && _currentDimensions.current.height !== dimensions.height) {
      _currentDimensions.current = dimensions;
      _setSize(dimensions);
    }
  }, [dimension]);
  useEffect(function () {
    if (container != null) {
      var observer = makeResizeObserver(container, function (_ref4) {
        var _ref5 = _slicedToArray(_ref4, 1),
          entry = _ref5[0];
        var _entry$borderBoxSize$2 = entry.borderBoxSize[0],
          inlineSize = _entry$borderBoxSize$2.inlineSize,
          blockSize = _entry$borderBoxSize$2.blockSize;
        setSize({
          width: inlineSize,
          height: blockSize
        });
      });
      return function () {
        return observer && observer.disconnect();
      };
    } else {
      setSize({
        width: 0,
        height: 0
      });
    }
  }, [container, setSize]);
  return size;
};