"use strict";

exports.__esModule = true;
exports.adjustOverlayPosition = adjustOverlayPosition;
function adjustOverlayPosition(overlay, padding) {
  var rect = overlay.getBoundingClientRect();
  var style = getComputedStyle(overlay);
  var windowWidth = window.innerWidth;
  var _ref = padding || [0],
    _ref$ = _ref[0],
    paddingTop = _ref$ === void 0 ? 0 : _ref$;
  var _ref2 = padding || [0],
    _ref2$ = _ref2[1],
    paddingRight = _ref2$ === void 0 ? paddingTop : _ref2$,
    _ref2$2 = _ref2[2],
    paddingBottom = _ref2$2 === void 0 ? paddingTop : _ref2$2;
  var _ref3 = padding || [0],
    _ref3$ = _ref3[3],
    paddingLeft = _ref3$ === void 0 ? paddingRight || paddingTop : _ref3$;
  var newTop = rect.top;
  var newLeft = rect.left;

  // 检查并修正左侧和右侧
  if (rect.left < 0) {
    newLeft = paddingLeft;
    console.debug('超出可视区域', 'left');
  } else if (rect.right > windowWidth) {
    console.debug('超出可视区域', 'right');
    newLeft = windowWidth - rect.width - paddingRight;
  }

  // 检查并修正顶部和底部
  if (rect.top < 0) {
    newTop = paddingTop;
    console.debug('超出可视区域', 'top');
  } else if (style.bottom.includes('-')) {
    console.debug('超出可视区域', 'bottom');
    // 下方超出的话则反转展示在上方。
    newTop -= parseFloat(style.height) + paddingBottom;
  }

  // 应用修正后的位置
  overlay.style.left = newLeft + "px";
  overlay.style.top = newTop + "px";
}