"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _getExpectPlacement = _interopRequireDefault(require("./getExpectPlacement"));

var _autoAdjustPlacement = _interopRequireDefault(require("./autoAdjustPlacement"));

var _getContainerVisibleArea = _interopRequireDefault(require("./getContainerVisibleArea"));

var _getPlacementInContainer = _interopRequireDefault(require("./getPlacementInContainer"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

/*
 * @Author: sixgod
 * @Date: 2018-12-01 10:21:36
 * @Last Modified by: lianghaosen
 * @Last Modified time: 2020-01-14 14:57:14
 */
function setPlacement(el, container) {
  var align = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "top";
  var bindNode = arguments.length > 3 ? arguments[3] : undefined;
  var autoAdjust = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : "x y";
  var cover = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;

  if (!el instanceof HTMLElement) {
    throw new Error("el must be a HTMLElement");
  }

  if (!container instanceof HTMLElement) {
    throw new Error("container must a HTMLElement");
  }

  if (!bindNode instanceof HTMLElement) {
    throw new Error("bindNode must a HTMLElement");
  }

  var alignTypes = align.trim().split(/\s+/);
  var alignReg = /^(top|right|left|bottom)$/;

  if (!alignReg.test(alignTypes[0] || alignTypes[1] && !alignReg.test(alignTypes[1]))) {
    throw new Error("invalid align value");
  }

  if (typeof autoAdjust === "string" && autoAdjust != "") {
    autoAdjust = autoAdjust.trim().replace(/\s+/, " ");

    if (autoAdjust !== "x" && autoAdjust !== "y" && autoAdjust !== "x y" && autoAdjust !== "y x") {
      throw new Error("invalid autoAdjust value");
    }
  } else if (autoAdjust != false) {
    throw new Error("invalid autoAdjust value");
  }

  var placement = (0, _getExpectPlacement["default"])(el, bindNode, alignTypes);
  var adjust = {};

  if (autoAdjust) {
    var adjustRes = (0, _autoAdjustPlacement["default"])(el, placement, alignTypes, (0, _getContainerVisibleArea["default"])(container), bindNode, autoAdjust, cover);
    adjust = adjustRes.adjust;
    placement = adjustRes.placement;
  }

  placement = (0, _getPlacementInContainer["default"])(placement, container);
  return {
    placement: placement,
    adjust: adjust
  };
}

var _default = setPlacement;
exports["default"] = _default;