"use strict";exports.__esModule = true;exports.PopupHelper = void 0;var _globalObject = require("@skbkontur/global-object");

var _getDOMRect = require("../../lib/dom/getDOMRect");




















function getPositionObject(position) {
  var x = position.split(' ');

  return {
    direction: x[0],
    align: x[1]
  };
}

function getElementAbsoluteRect(element) {
  var rect = _getElementRelativeRect(element);
  return convertRectToAbsolute(rect);
}

function isAbsoluteRectFullyVisible(coordinates, popupRect) {
  var windowRelativeRect = _getWindowRelativeRect();
  var windowAbsoluteRect = convertRectToAbsolute(windowRelativeRect);
  var absoluteRect = {
    top: coordinates.top,
    left: coordinates.left,
    height: popupRect.height,
    width: popupRect.width
  };

  return _rectContainsRect(windowAbsoluteRect, absoluteRect);
}

// Can become fully visible by scrolling into viewport
function canBecomeFullyVisible(positionName, coordinates) {
  var position = getPositionObject(positionName);

  if (position.direction === 'top') {
    var availableScrollDistances = convertRectToAbsolute(_getWindowRelativeRect());
    return coordinates.top + availableScrollDistances.top >= 0;
  }

  if (position.direction === 'left') {
    var _availableScrollDistances = convertRectToAbsolute(_getWindowRelativeRect());
    return coordinates.left + _availableScrollDistances.left >= 0;
  }

  // NOTE: for bottom/right cases browser will always expand document size
  return true;
}

function _getElementRelativeRect(element) {
  var rect = (0, _getDOMRect.getDOMRect)(element);

  return {
    top: rect.top,
    left: rect.left,
    width: rect.right - rect.left,
    height: rect.bottom - rect.top
  };
}

function _getWindowRelativeRect() {
  return {
    top: 0,
    left: 0,
    width: _getViewProperty(function (x) {return x.clientWidth;}) || _globalObject.globalObject.innerWidth || 0,
    height: _getViewProperty(function (x) {return x.clientHeight;}) || _globalObject.globalObject.innerHeight || 0
  };
}

function convertRectToAbsolute(rect) {
  var offset = _getAbsoluteOffset();

  return {
    top: rect.top + offset.top,
    left: rect.left + offset.left,
    width: rect.width,
    height: rect.height
  };
}

function _getAbsoluteOffset() {
  var scrollTop = _globalObject.globalObject.pageYOffset || _getViewProperty(function (x) {return x.scrollTop;});
  var scrollLeft = _globalObject.globalObject.pageXOffset || _getViewProperty(function (x) {return x.scrollLeft;});

  var clientTop = _getViewProperty(function (x) {return x.clientTop;});
  var clientLeft = _getViewProperty(function (x) {return x.clientLeft;});

  var top = scrollTop - clientTop;
  var left = scrollLeft - clientLeft;

  return {
    top: top,
    left: left
  };
}

function _rectContainsRect(outerRect, innerRect) {
  return (
    innerRect.top > outerRect.top &&
    innerRect.top + innerRect.height < outerRect.top + outerRect.height &&
    innerRect.left > outerRect.left &&
    innerRect.left + innerRect.width < outerRect.left + outerRect.width);

}

function _getViewProperty(getProperty) {var _globalObject$documen, _globalObject$documen2;
  var views = [(_globalObject$documen = _globalObject.globalObject.document) == null ? void 0 : _globalObject$documen.documentElement, (_globalObject$documen2 = _globalObject.globalObject.document) == null ? void 0 : _globalObject$documen2.body];
  return views.map(function (x) {return x && getProperty(x);}).find(Boolean) || 0;
}

var PopupHelper = exports.PopupHelper = {
  getPositionObject: getPositionObject,
  getElementAbsoluteRect: getElementAbsoluteRect,
  isFullyVisible: isAbsoluteRectFullyVisible,
  canBecomeFullyVisible: canBecomeFullyVisible
};