"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;

/*
 * @Author: lianghaosen
 * @Date: 2018-12-01 10:48:07
 * @Last Modified by: lianghaosen
 * @Last Modified time: 2020-01-14 15:20:33
 */
function doAdjust(dire, el, expectPlacement, alignType, visibleArea, bindNode, adjustedPlacement, cover) {
  var bindNodeOffset = bindNode.getBoundingClientRect();
  var elOffset = el.getBoundingClientRect();
  var direction = {
    x: ["left", "right", "width"],
    y: ["top", "bottom", "height"]
  };
  var d = direction[dire];
  var adjust = null; // 记录调整方式 翻转或移动
  // 调整横向与left right width有关
  // 调整纵向与top bottom hright有关
  // d[0]即left/top d[1]即right/bottom d[2]即width/height

  if (alignType[0] === d[0]) {
    // 如果定位到左/上侧 左/上侧空间不足且允许覆盖点击元素
    if (cover && expectPlacement[d[0]] < visibleArea[d[0]]) {
      adjustedPlacement[d[0]] = visibleArea[d[0]];
      adjust = "move";
    } // 如果定位到左/上侧 左/上侧空间不足且右/下侧空间足够则翻转到右/下侧
    else if (expectPlacement[d[0]] < visibleArea[d[0]] && elOffset[d[2]] <= visibleArea[d[1]] - bindNodeOffset[d[1]]) {
      adjustedPlacement[d[0]] = bindNodeOffset[d[1]];
      adjust = "reverse";
    }
  } else if (alignType[0] === d[1]) {
    // 如果定位到右/下侧 右/下侧空间不足且允许覆盖点击元素
    if (cover && expectPlacement[d[1]] > visibleArea[d[1]]) {
      adjustedPlacement[d[0]] = visibleArea[d[1]] - elOffset[d[2]];
      adjust = "move";
    } // 如果定位到右/下侧 右/下侧空间不足且左/上侧空间足够则翻转到左/上侧
    else if (expectPlacement[d[1]] > visibleArea[d[1]] && bindNodeOffset[d[0]] - visibleArea[d[0]] >= elOffset[d[2]]) {
      adjustedPlacement[d[0]] = bindNodeOffset[d[0]] - elOffset[d[2]];
      adjust = "reverse";
    }
  } else {
    // 调整交叉轴位置
    if (expectPlacement[d[0]] < visibleArea[d[0]]) {
      adjustedPlacement[d[0]] = visibleArea[d[0]];
      adjust = "move";
    }

    if (adjustedPlacement[d[0]] + elOffset[d[2]] > visibleArea[d[1]]) {
      adjustedPlacement[d[0]] = visibleArea[d[1]] - elOffset[d[2]];
      adjust = "move";
    }
  }
}
/**
  * @param {DOMElement} el
  * @param {Object} expectPlacement
  * @param {String} align
  * @param {Object} visibleArea
  * @param {DOMElement} bindNode
  * @param {String} adjustType
  */


function _default(el, expectPlacement, align, visibleArea, bindNode, adjustType, cover) {
  var adjustedPlacement = {
    left: expectPlacement.left,
    top: expectPlacement.top
  };
  var adjustx = null;
  var adjusty = null;

  if (adjustType === "x" || adjustType === "x y") {
    adjustx = doAdjust("x", el, expectPlacement, align, visibleArea, bindNode, adjustedPlacement, cover);
  }

  if (adjustType === "y" || adjustType === "x y") {
    adjusty = doAdjust("y", el, expectPlacement, align, visibleArea, bindNode, adjustedPlacement, cover);
  }

  return {
    adjust: {
      x: adjustx,
      y: adjusty
    },
    placement: adjustedPlacement
  };
}