'use strict';

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

var getBasePlacement = require('../utils/getBasePlacement.js');
var getLayoutRect = require('../dom-utils/getLayoutRect.js');
var contains = require('../dom-utils/contains.js');
var getOffsetParent = require('../dom-utils/getOffsetParent.js');
var getMainAxisFromPlacement = require('../utils/getMainAxisFromPlacement.js');
var within = require('../utils/within.js');
var mergePaddingObject = require('../utils/mergePaddingObject.js');
var expandToHashMap = require('../utils/expandToHashMap.js');
var enums = require('../enums.js');

var toPaddingObject = function toPaddingObject(padding, state) {
  padding = typeof padding === 'function' ? padding(Object.assign({}, state.rects, {
    placement: state.placement
  })) : padding;
  return mergePaddingObject.default(typeof padding !== 'number' ? padding : expandToHashMap.default(padding, enums.basePlacements));
};

function arrow$1(_ref) {
  var _state$modifiersData$;

  var state = _ref.state,
      name = _ref.name,
      options = _ref.options;
  var arrowElement = state.elements.arrow;
  var popperOffsets = state.modifiersData.popperOffsets;
  var basePlacement = getBasePlacement.default(state.placement);
  var axis = getMainAxisFromPlacement.default(basePlacement);
  var isVertical = [enums.left, enums.right].indexOf(basePlacement) >= 0;
  var len = isVertical ? 'height' : 'width';

  if (!arrowElement || !popperOffsets) {
    return;
  }

  var paddingObject = toPaddingObject(options.padding, state);
  var arrowRect = getLayoutRect.default(arrowElement);
  var minProp = axis === 'y' ? enums.top : enums.left;
  var maxProp = axis === 'y' ? enums.bottom : enums.right;
  var endDiff = state.rects.reference[len] + state.rects.reference[axis] - popperOffsets[axis] - state.rects.popper[len];
  var startDiff = popperOffsets[axis] - state.rects.reference[axis];
  var arrowOffsetParent = getOffsetParent.default(arrowElement);
  var clientSize = arrowOffsetParent ? axis === 'y' ? arrowOffsetParent.clientHeight || 0 : arrowOffsetParent.clientWidth || 0 : 0;
  var centerToReference = endDiff / 2 - startDiff / 2; // Make sure the arrow doesn't overflow the popper if the center point is
  // outside of the popper bounds

  var min = paddingObject[minProp];
  var max = clientSize - arrowRect[len] - paddingObject[maxProp];
  var center = clientSize / 2 - arrowRect[len] / 2 + centerToReference;
  var offset = within.within(min, center, max); // Prevents breaking syntax highlighting...

  var axisProp = axis;
  state.modifiersData[name] = (_state$modifiersData$ = {}, _state$modifiersData$[axisProp] = offset, _state$modifiersData$.centerOffset = offset - center, _state$modifiersData$);
}

function effect(_ref2) {
  var state = _ref2.state,
      options = _ref2.options;
  var _options$element = options.element,
      arrowElement = _options$element === void 0 ? '[data-popper-arrow]' : _options$element;

  if (arrowElement == null) {
    return;
  } // CSS selector


  if (typeof arrowElement === 'string') {
    arrowElement = state.elements.popper.querySelector(arrowElement);

    if (!arrowElement) {
      return;
    }
  }

  if (!contains.default(state.elements.popper, arrowElement)) {
    return;
  }

  state.elements.arrow = arrowElement;
} // eslint-disable-next-line import/no-unused-modules


var arrow = {
  name: 'arrow',
  enabled: true,
  phase: 'main',
  fn: arrow$1,
  effect: effect,
  requires: ['popperOffsets'],
  requiresIfExists: ['preventOverflow']
};

exports.default = arrow;
//# sourceMappingURL=arrow.js.map
