"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.getAppendToElement = getAppendToElement;
exports.getByPredicate = getByPredicate;
exports.popoverHostElementId = void 0;
var _utils = require("./utils");
var _isElement = _interopRequireDefault(require("lodash/isElement"));
var popoverHostElementId = exports.popoverHostElementId = 'wsr-popover-host';
function getAppendToElement(appendTo, node) {
  if (!appendTo) {
    return null;
  }
  if (typeof appendTo === 'string') {
    switch (appendTo) {
      case 'window':
      case 'viewport':
        return getPopoverHostElement();
      case 'scrollParent':
        // @ts-ignore
        return (0, _utils.getScrollParent)(node);
      default:
        return;
    }
  }
  if (typeof appendTo === 'function') {
    return getByPredicate(appendTo, node);
  }
  if ((0, _isElement.default)(appendTo)) {
    return appendTo;
  }
  return;
}
function getPopoverHostElement() {
  var host = document.querySelector("#".concat(popoverHostElementId));
  if (host === null) {
    var newElement = document.createElement('div');
    newElement.setAttribute('id', popoverHostElementId);
    document.body.appendChild(newElement);
    return newElement;
  }
  return host;
}

// @ts-ignore
function getByPredicate(predicate, element) {
  if (!element) {
    return;
  }
  var node = searchParent(predicate, element);
  if (node) {
    return node;
  }
  return;
}

// @ts-ignore
function searchParent(predicate, element) {
  if (!element) {
    return;
  }
  if (predicate(element)) {
    return element;
  }
  return searchParent(predicate, (0, _utils.getParentNode)(element));
}
//# sourceMappingURL=getAppendToElement.js.map