'use strict';

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

var getVariation = require('./getVariation.js');
var enums = require('../enums.js');
var detectOverflow = require('./detectOverflow.js');
var getBasePlacement = require('./getBasePlacement.js');

function computeAutoPlacement(state, options) {
  if (options === void 0) {
    options = {};
  }

  var _options = options,
      placement = _options.placement,
      boundary = _options.boundary,
      rootBoundary = _options.rootBoundary,
      padding = _options.padding,
      flipVariations = _options.flipVariations,
      _options$allowedAutoP = _options.allowedAutoPlacements,
      allowedAutoPlacements = _options$allowedAutoP === void 0 ? enums.placements : _options$allowedAutoP;
  var variation = getVariation.default(placement);
  var placements = variation ? flipVariations ? enums.variationPlacements : enums.variationPlacements.filter(function (placement) {
    return getVariation.default(placement) === variation;
  }) : enums.basePlacements;
  var allowedPlacements = placements.filter(function (placement) {
    return allowedAutoPlacements.indexOf(placement) >= 0;
  });

  if (allowedPlacements.length === 0) {
    allowedPlacements = placements;
  } // $FlowFixMe[incompatible-type]: Flow seems to have problems with two array unions...


  var overflows = allowedPlacements.reduce(function (acc, placement) {
    acc[placement] = detectOverflow.default(state, {
      placement: placement,
      boundary: boundary,
      rootBoundary: rootBoundary,
      padding: padding
    })[getBasePlacement.default(placement)];
    return acc;
  }, {});
  return Object.keys(overflows).sort(function (a, b) {
    return overflows[a] - overflows[b];
  });
}

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