"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
exports.findIFrame = findIFrame;
exports.getDocument = getDocument;
exports.getDocuments = getDocuments;
exports.getMousePosition = getMousePosition;
exports.transformZoomData = void 0;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var zoom = 0;

function getDocument(self) {
  try {
    var parent = self.parent,
        top = self.top;

    if (parent !== top) {
      return getDocument(parent);
    }

    return parent.document;
  } catch (e) {
    return self.document;
  }
}

function getDocuments(self) {
  var list = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

  try {
    var parent = self.parent,
        top = self.top;

    if (parent !== top) {
      return getDocuments(parent, list);
    }

    return list.concat(parent.document);
  } catch (e) {
    return list.concat(self.document);
  }
}

function findIFrame(self) {
  return (0, _toConsumableArray2["default"])(self.parent.document.querySelectorAll('iframe')).find(function (frame) {
    return frame.contentWindow === self;
  });
}

function getPageMousePosition(x, y, self, client) {
  var _self$document$docume = self.document.documentElement,
      scrollTop = _self$document$docume.scrollTop,
      scrollLeft = _self$document$docume.scrollLeft,
      clientWidth = _self$document$docume.clientWidth,
      clientHeight = _self$document$docume.clientHeight;

  if (client) {
    return {
      x: x,
      y: y,
      vw: clientWidth,
      vh: clientHeight
    };
  }

  return {
    x: x + scrollLeft,
    y: y + scrollTop,
    vw: clientWidth,
    vh: clientHeight
  };
}

function getMousePosition(x, y, self, client) {
  try {
    if (self.top === self) {
      return getPageMousePosition(x, y, self, client);
    }

    var parent = self.parent;
    var iframe = findIFrame(self);

    var _ref = iframe ? iframe.getBoundingClientRect() : {
      top: 0,
      left: 0
    },
        top = _ref.top,
        left = _ref.left;

    var newX = x + left;
    var newY = y + top;

    if (parent === self.top) {
      return getPageMousePosition(newX, newY, parent, client);
    }

    return getMousePosition(newX, newY, parent);
  } catch (e) {
    return getPageMousePosition(x, y, self, client);
  }
}
/**
 * 转换缩放比例后的数据
 * 场景：兼容全局带有 zoom 样式导致的数据偏差
 * @param data 需要转换的数据
 * @returns 转换后的数据
 */


var transformZoomData = function transformZoomData(data) {
  if (!zoom) {
    // eslint-disable-next-line dot-notation
    zoom = Number(getComputedStyle(document.body)['zoom']) || 1;
  }

  return data / zoom;
};

exports.transformZoomData = transformZoomData;
var _default = {
  getDocument: getDocument,
  getDocuments: getDocuments,
  findIFrame: findIFrame,
  getMousePosition: getMousePosition,
  transformZoomData: transformZoomData
};
exports["default"] = _default;
//# sourceMappingURL=index.js.map
