function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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