import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
/*
 * 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 { 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.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;
};