'use strict';

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

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');

var allowedPlacements = ['top-start', 'top-end', 'bottom-start', 'bottom-end', 'left-start', 'left-end', 'right-start', 'right-end'];
var widthThreshold = 28;
var offsetXStartThreshold = 17;
var offsetXEndThreshold = 19;
var usePopoverOffset = function usePopoverOffset(_ref) {
  var handleRef = _ref.handleRef,
    placement = _ref.placement,
    offsetOuter = _ref.offsetOuter;
  var _useState = React.useState(offsetOuter),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    offset = _useState2[0],
    setOffset = _useState2[1];
  React.useEffect(function () {
    var refElement = handleRef.current;
    if (!refElement || !allowedPlacements.includes(placement)) {
      setOffset(offsetOuter);
      return;
    }
    var _refElement$getBoundi = refElement.getBoundingClientRect(),
      width = _refElement$getBoundi.width,
      height = _refElement$getBoundi.height;
    var isVertical = (placement === null || placement === void 0 ? void 0 : placement.startsWith('top')) || (placement === null || placement === void 0 ? void 0 : placement.startsWith('bottom'));
    var isEnd = placement === null || placement === void 0 ? void 0 : placement.endsWith('end');
    var isStart = placement === null || placement === void 0 ? void 0 : placement.endsWith('start');
    var offsetX = offsetOuter[0];
    var offsetY = offsetOuter[1];
    if (isVertical) {
      if ((isStart || isEnd) && width < widthThreshold) {
        offsetX = isEnd ? offsetXStartThreshold - width / 2 + offsetOuter[0] : -offsetXStartThreshold + width / 2 - offsetOuter[0];
      }
    }
    if (!isVertical && (isStart || isEnd) && height < widthThreshold) {
      offsetX = isStart ? -offsetXEndThreshold + height / 2 : offsetXEndThreshold - height / 2;
    }
    setOffset([offsetX, offsetY]);
  }, [handleRef, placement, offsetOuter[0], offsetOuter[1]]);
  return offset;
};

exports.usePopoverOffset = usePopoverOffset;
