import { getParentNode, getScrollParent } from './utils';
import isElement from 'lodash/isElement';
export const popoverHostElementId = 'wsr-popover-host';
export 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 getScrollParent(node);
            default:
                return;
        }
    }
    if (typeof appendTo === 'function') {
        return getByPredicate(appendTo, node);
    }
    if (isElement(appendTo)) {
        return appendTo;
    }
    return;
}
function getPopoverHostElement() {
    const host = document.querySelector(`#${popoverHostElementId}`);
    if (host === null) {
        const newElement = document.createElement('div');
        newElement.setAttribute('id', popoverHostElementId);
        document.body.appendChild(newElement);
        return newElement;
    }
    return host;
}
// @ts-ignore
export function getByPredicate(predicate, element) {
    if (!element) {
        return;
    }
    const 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, getParentNode(element));
}
//# sourceMappingURL=getAppendToElement.js.map