'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var getBoundingClientRect = require('./getBoundingClientRect.js');
var getNodeScroll = require('./getNodeScroll.js');
var getNodeName = require('./getNodeName.js');
var instanceOf = require('./instanceOf.js');
var getWindowScrollBarX = require('./getWindowScrollBarX.js');
var getDocumentElement = require('./getDocumentElement.js');
var isScrollParent = require('./isScrollParent.js');
var math = require('../utils/math.js');

function isElementScaled(element) {
  var rect = element.getBoundingClientRect();
  var scaleX = math.round(rect.width) / element.offsetWidth || 1;
  var scaleY = math.round(rect.height) / element.offsetHeight || 1;
  return scaleX !== 1 || scaleY !== 1;
} // Returns the composite rect of an element relative to its offsetParent.
// Composite means it takes into account transforms as well as layout.


function getCompositeRect(elementOrVirtualElement, offsetParent, isFixed) {
  if (isFixed === void 0) {
    isFixed = false;
  }

  var isOffsetParentAnElement = instanceOf.isHTMLElement(offsetParent);
  var offsetParentIsScaled = instanceOf.isHTMLElement(offsetParent) && isElementScaled(offsetParent);
  var documentElement = getDocumentElement.default(offsetParent);
  var rect = getBoundingClientRect.default(elementOrVirtualElement, offsetParentIsScaled, isFixed);
  var scroll = {
    scrollLeft: 0,
    scrollTop: 0
  };
  var offsets = {
    x: 0,
    y: 0
  };

  if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
    if (getNodeName.default(offsetParent) !== 'body' || // https://github.com/popperjs/popper-core/issues/1078
    isScrollParent.default(documentElement)) {
      scroll = getNodeScroll.default(offsetParent);
    }

    if (instanceOf.isHTMLElement(offsetParent)) {
      offsets = getBoundingClientRect.default(offsetParent, true);
      offsets.x += offsetParent.clientLeft;
      offsets.y += offsetParent.clientTop;
    } else if (documentElement) {
      offsets.x = getWindowScrollBarX.default(documentElement);
    }
  }

  return {
    x: rect.left + scroll.scrollLeft - offsets.x,
    y: rect.top + scroll.scrollTop - offsets.y,
    width: rect.width,
    height: rect.height
  };
}

exports.default = getCompositeRect;
//# sourceMappingURL=getCompositeRect.js.map
