function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { defaultRect } from './dom';
var optionHeight = 36;

// UTILS

function getComponents(placement) {
  var _placement$split = placement.split('-'),
    _placement$split2 = _slicedToArray(_placement$split, 2),
    primary = _placement$split2[0],
    secondary = _placement$split2[1];
  var edge = primary;
  var align = secondary || 'center';
  return {
    edge: edge,
    align: align
  };
}
function fromComponents(_ref) {
  var align = _ref.align,
    edge = _ref.edge;
  return align === 'center' ? edge : "".concat(edge, "-").concat(align);
}
function getContainer() {
  return {
    top: 0,
    left: 0,
    width: window.innerWidth,
    height: window.innerHeight,
    bottom: window.innerWidth,
    right: window.innerWidth
  };
}
function flip(current) {
  var opposite = {
    top: 'bottom',
    bottom: 'top',
    left: 'right',
    right: 'left',
    center: 'center'
  };
  return opposite[current];
}

// GENERIC POSITION UPDATER (Edge, Align)
// Flip updater if validator fails and opposite passes
function getPositionComponent(_ref2) {
  var validator = _ref2.validator,
    updater = _ref2.updater;
  return function (_ref3) {
    var container = _ref3.container,
      target = _ref3.target,
      overlay = _ref3.overlay,
      current = _ref3.placement,
      _ref3$canFlip = _ref3.canFlip,
      canFlip = _ref3$canFlip === void 0 ? true : _ref3$canFlip;
    var opposite = flip(current);
    var shouldFlip = !validator[current](container, target, overlay) && validator[opposite](container, target, overlay);
    var placement = canFlip && shouldFlip ? opposite : current;
    return _objectSpread({
      placement: placement
    }, updater[placement](target, overlay));
  };
}

// PRIMARY
var Edge = {
  updater: {
    left: function left(target, overlay) {
      return {
        left: target.left - overlay.width
      };
    },
    right: function right(target, overlay) {
      return {
        left: target.right
      };
    },
    top: function top(target, overlay) {
      return {
        top: target.top - overlay.height
      };
    },
    bottom: function bottom(target, overlay) {
      return {
        top: target.bottom
      };
    }
  },
  validator: {
    top: function top(container, target, overlay) {
      return target.top - container.top >= overlay.height;
    },
    left: function left(container, target, overlay) {
      return target.left - container.left >= overlay.width;
    },
    bottom: function bottom(container, target, overlay) {
      return container.height - target.bottom >= overlay.height;
    },
    right: function right(container, target, overlay) {
      return container.width - target.right >= overlay.width;
    }
  }
};

// SECONDARY
export var Alignment = {
  updater: {
    left: function left(target, overlay) {
      return {
        left: target.left
      };
    },
    right: function right(target, overlay) {
      return {
        left: target.right - overlay.width
      };
    },
    top: function top(target, overlay) {
      return {
        top: target.top
      };
    },
    bottom: function bottom(target, overlay) {
      return {
        top: target.bottom - overlay.height
      };
    },
    center: function center(target, overlay) {
      return {
        left: target.left + (target.width - overlay.width) / 2,
        top: target.top + (target.height - overlay.height) / 2
      };
    }
  },
  validator: {
    top: function top(container, target, overlay) {
      return container.height - target.top >= overlay.height;
    },
    right: function right(container, target, overlay) {
      return target.right - container.left >= overlay.width;
    },
    left: function left(container, target, overlay) {
      return container.width - target.left >= overlay.width;
    },
    bottom: function bottom(container, target, overlay) {
      return target.bottom - container.top >= overlay.height;
    },
    center: function center() {
      return true;
    }
  }
};
export var getAlignmentPosition = getPositionComponent(Alignment);
export var getEdgePosition = getPositionComponent(Edge);
export var getFlyoutAlignmentPosition = getPositionComponent({
  updater: _objectSpread(_objectSpread({}, Alignment.updater), {}, {
    left: function left(target, overlay) {
      return {
        left: target.right
      };
    },
    right: function right(target, overlay) {
      return {
        left: target.left - overlay.width
      };
    },
    center: function center(target, overlay) {
      return {
        left: target.right,
        top: target.top + (target.height - overlay.height) / 2
      };
    }
  }),
  validator: Alignment.validator
});
export var getFlyoutEdgePosition = getPositionComponent({
  updater: _objectSpread(_objectSpread({}, Edge.updater), {}, {
    bottom: function bottom(target, overlay) {
      return {
        top: target.bottom - optionHeight
      };
    } // Aligns to height of menu option
  }),

  validator: Edge.validator
});
export var padding = function padding() {
  var pad = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  var shape = arguments.length > 1 ? arguments[1] : undefined;
  return _objectSpread(_objectSpread({}, shape), {}, {
    width: shape.width + pad * 2,
    height: shape.height + pad * 2,
    top: shape.top - pad,
    bottom: shape.bottom + pad,
    left: shape.left - pad,
    right: shape.right + pad
  });
};
export function getAnchorPosition(placement) {
  var pad = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var container = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : getContainer();
  var target = arguments.length > 3 ? arguments[3] : undefined;
  var overlay = arguments.length > 4 ? arguments[4] : undefined;
  var canFlip = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : true;
  var isFlyout = arguments.length > 6 ? arguments[6] : undefined;
  if (!target || !overlay) {
    return defaultRect;
  }
  var components = getComponents(placement);
  var defaultConfig = {
    canFlip: canFlip,
    container: container,
    target: target,
    overlay: overlay,
    placement: placement
  };
  var isFlyoutDefaultButton = target.width === 24 || target.width === 36 || target.width === 48;
  var isFlyoutAdjustment = isFlyout && !isFlyoutDefaultButton;
  var align = isFlyoutAdjustment ? getFlyoutAlignmentPosition(_objectSpread(_objectSpread({}, defaultConfig), {}, {
    placement: components.align
  })) : getAlignmentPosition(_objectSpread(_objectSpread({}, defaultConfig), {}, {
    placement: components.align
  }));
  var edge = isFlyoutAdjustment ? getFlyoutEdgePosition(_objectSpread(_objectSpread({}, defaultConfig), {}, {
    target: padding(pad, target),
    placement: components.edge
  })) : getEdgePosition(_objectSpread(_objectSpread({}, defaultConfig), {}, {
    target: padding(pad, target),
    placement: components.edge
  }));
  return _objectSpread(_objectSpread(_objectSpread({}, align), edge), {}, {
    placement: fromComponents({
      align: align.placement,
      edge: edge.placement
    }),
    minWidth: target.width
  });
}
//# sourceMappingURL=getAnchorPosition.js.map