import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.includes.js';
import 'core-js/modules/es.array.join.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.string.includes.js';
import { settings } from '../../constants/Settings.js';

var prefix = settings.prefix;
var getBoundingClientRects = function getBoundingClientRects(buttonRef, isSplitButton) {
  var _buttonRef$current, _buttonRef$current$ne, _buttonRef$current2;
  var buttonRect = buttonRef.current.getBoundingClientRect();
  var primaryButtonRect = isSplitButton ? buttonRef === null || buttonRef === void 0 || (_buttonRef$current = buttonRef.current) === null || _buttonRef$current === void 0 || (_buttonRef$current = _buttonRef$current.previousSibling) === null || _buttonRef$current === void 0 ? void 0 : _buttonRef$current.getBoundingClientRect() : null;
  // Once the menuButton can accept a target for the react portal, we can
  // use this method again once the menu is placed within the same div as the
  // button.
  // const menuRect = buttonRef.current.nextSibling?.getBoundingClientRect();
  var node = (_buttonRef$current$ne = (_buttonRef$current2 = buttonRef.current) === null || _buttonRef$current2 === void 0 ? void 0 : _buttonRef$current2.nextSibling) !== null && _buttonRef$current$ne !== void 0 ? _buttonRef$current$ne : document.querySelector(".".concat(prefix, "--menu.").concat(prefix, "--menu--open.").concat(prefix, "--menu--root"));
  var menuRect = node === null || node === void 0 ? void 0 : node.getBoundingClientRect();
  return {
    buttonRect: buttonRect,
    primaryButtonRect: primaryButtonRect,
    menuRect: menuRect
  };
};
var getMenuDimensions = function getMenuDimensions(menuRect) {
  var _menuRect$height, _menuRect$width;
  return {
    menuHeight: (_menuRect$height = menuRect === null || menuRect === void 0 ? void 0 : menuRect.height) !== null && _menuRect$height !== void 0 ? _menuRect$height : 0,
    menuWidth: (_menuRect$width = menuRect === null || menuRect === void 0 ? void 0 : menuRect.width) !== null && _menuRect$width !== void 0 ? _menuRect$width : 0
  };
};
var getOverflow = function getOverflow(menuRect, buttonRect) {
  var windowWidth = window.innerWidth || document.documentElement.clientWidth;
  var windowHeight = window.innerHeight || document.documentElement.clientHeight;
  var _getMenuDimensions = getMenuDimensions(menuRect),
    menuHeight = _getMenuDimensions.menuHeight,
    menuWidth = _getMenuDimensions.menuWidth;
  var offTop = buttonRect.top - menuHeight < 0;
  var offLeft = buttonRect.left - menuWidth < 0;
  var offRight = buttonRect.right + menuWidth > windowWidth;
  var offBottom = buttonRect.bottom + menuHeight > windowHeight;
  var T = offTop ? 'top' : '';
  var R = offRight ? 'right' : '';
  var B = offBottom ? 'bottom' : '';
  var L = offLeft ? 'left' : '';
  return [T, R, B, L].filter(Boolean).join('-');
};
var getShadowBlockerConfig = function getShadowBlockerConfig(buttonRef) {
  var _getBoundingClientRec = getBoundingClientRects(buttonRef),
    buttonRect = _getBoundingClientRec.buttonRect,
    menuRect = _getBoundingClientRec.menuRect;
  var _getMenuDimensions2 = getMenuDimensions(menuRect),
    menuHeight = _getMenuDimensions2.menuHeight;
  var overflow = getOverflow(menuRect, buttonRect);
  var flippedX = (overflow === null || overflow === void 0 ? void 0 : overflow.includes('bottom')) && !(overflow !== null && overflow !== void 0 && overflow.includes('top'));
  var flippedY = overflow === null || overflow === void 0 ? void 0 : overflow.includes('right');
  var opensHorizontally = (overflow === null || overflow === void 0 ? void 0 : overflow.includes('top')) && (overflow === null || overflow === void 0 ? void 0 : overflow.includes('bottom'));
  return {
    menuHeight: menuHeight,
    flippedX: flippedX,
    flippedY: flippedY,
    opensHorizontally: opensHorizontally
  };
};
var getMenuPosition = function getMenuPosition(_ref) {
  var label = _ref.label,
    buttonRef = _ref.buttonRef,
    onPrimaryActionClick = _ref.onPrimaryActionClick,
    langDir = _ref.langDir;
  var isSplitButton = label && typeof onPrimaryActionClick === 'function';
  var _getBoundingClientRec2 = getBoundingClientRects(buttonRef, isSplitButton),
    buttonRect = _getBoundingClientRec2.buttonRect,
    primaryButtonRect = _getBoundingClientRec2.primaryButtonRect,
    menuRect = _getBoundingClientRec2.menuRect;
  var isRtl = langDir === 'rtl';
  var buttonX = buttonRect.x,
    buttonY = buttonRect.y,
    buttonWidth = buttonRect.width,
    buttonHeight = buttonRect.height;
  var bodyHeight = document.body.clientHeight;
  var _ref2 = primaryButtonRect !== null && primaryButtonRect !== void 0 ? primaryButtonRect : {
      width: 0
    },
    primaryButtonWidth = _ref2.width;
  var hasScrollY = document.body.scrollHeight >= bodyHeight;
  var scrollXOffset = hasScrollY ? 15 : 0;
  var y = buttonY + buttonHeight;
  var x = isSplitButton ? buttonX + primaryButtonWidth : buttonX;
  var _getMenuDimensions3 = getMenuDimensions(menuRect),
    menuHeight = _getMenuDimensions3.menuHeight,
    menuWidth = _getMenuDimensions3.menuWidth;
  var overflow = getOverflow(menuRect, buttonRect);

  /* istanbul ignore else */
  if (menuRect) {
    switch (overflow) {
      case 'top-right-bottom':
        x = isSplitButton ? isRtl ? buttonRect.left - menuWidth + scrollXOffset : primaryButtonRect.left - menuWidth : buttonX;
        y = buttonY;
        break;
      case 'top-bottom-left':
      case 'top-bottom':
        x = isSplitButton ? isRtl ? primaryButtonRect.right + scrollXOffset : buttonX + buttonWidth : buttonX + buttonWidth;
        y = buttonY;
        break;
      case 'top-left':
        x = isSplitButton ? isRtl ? buttonX : buttonX - primaryButtonWidth : buttonX;
        y = buttonY + buttonHeight;
        break;
      case 'top-right':
        x = isSplitButton ? isRtl ? buttonX + buttonWidth + primaryButtonWidth : buttonX - primaryButtonWidth - buttonWidth : buttonX + buttonWidth;
        y = buttonY + buttonHeight;
        break;
      case 'right-bottom':
        x = isSplitButton ? isRtl ? buttonX + buttonWidth + primaryButtonWidth : buttonX + buttonWidth : buttonX + buttonWidth;
        y = buttonY - menuHeight;
        break;
      case 'bottom-left':
      case 'bottom':
        x = isSplitButton ? isRtl ? buttonX : buttonX - primaryButtonWidth : buttonX;
        y = buttonY - menuHeight;
        break;
      case 'top':
      case 'left':
        x = isSplitButton ? isRtl ? buttonX : buttonX - primaryButtonWidth : buttonX;
        y = buttonY + buttonHeight;
        break;
      case 'right':
        x = isSplitButton ? isRtl ? buttonX + buttonWidth + primaryButtonWidth : buttonX + buttonWidth : buttonX + buttonWidth;
        y = buttonY + buttonHeight;
        break;
      default:
        x = isSplitButton ? isRtl ? buttonX : buttonX - primaryButtonWidth : buttonX;
        y = buttonY + buttonHeight;
    }
    if (isRtl) {
      x -= scrollXOffset;
    }
  }
  return {
    x: x,
    y: y
  };
};

export { getMenuPosition, getShadowBlockerConfig };
