"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isSamePoint = isSamePoint;
exports.monitorResize = monitorResize;
exports.restoreFocus = restoreFocus;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _contains = _interopRequireDefault(require("../vc-util/Dom/contains"));
var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
var _useShadowRoot = _interopRequireDefault(require("../_util/hooks/useShadowRoot"));
function isSamePoint(prev, next) {
  if (prev === next) return true;
  if (!prev || !next) return false;
  if ('pageX' in next && 'pageY' in next) {
    return prev.pageX === next.pageX && prev.pageY === next.pageY;
  }
  if ('clientX' in next && 'clientY' in next) {
    return prev.clientX === next.clientX && prev.clientY === next.clientY;
  }
  return false;
}
function restoreFocus(activeElement, container) {
  // Focus back if is in the container
  if (activeElement !== document.activeElement && (0, _contains.default)(container, activeElement) && typeof activeElement.focus === 'function') {
    activeElement.focus();
  }
}
function monitorResize(element, callback) {
  var prevWidth = null;
  var prevHeight = null;
  function onResize(_ref) {
    var _ref2 = (0, _slicedToArray2.default)(_ref, 1),
      target = _ref2[0].target;
    var shadowRoot = (0, _useShadowRoot.default)();
    var isTargetExist = shadowRoot ? shadowRoot.contains(target) : document.documentElement.contains(target);
    if (!isTargetExist) return;
    var _target$getBoundingCl = target.getBoundingClientRect(),
      width = _target$getBoundingCl.width,
      height = _target$getBoundingCl.height;
    var fixedWidth = Math.floor(width);
    var fixedHeight = Math.floor(height);
    if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {
      // https://webkit.org/blog/9997/resizeobserver-in-webkit/
      Promise.resolve().then(function () {
        callback({
          width: fixedWidth,
          height: fixedHeight
        });
      });
    }
    prevWidth = fixedWidth;
    prevHeight = fixedHeight;
  }
  var resizeObserver = new _resizeObserverPolyfill.default(onResize);
  if (element) {
    resizeObserver.observe(element);
  }
  return function () {
    resizeObserver.disconnect();
  };
}