"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get findTabbableDescendants () {
        return findTabbableDescendants;
    },
    get focusSelector () {
        return focusSelector;
    },
    get isFocusable () {
        return isFocusable;
    },
    get isTabble () {
        return isTabble;
    },
    get isVisible () {
        return isVisible;
    }
});
const tabbableNode = /input|select|textarea|button|object/;
const focusSelector = [
    'input',
    'select',
    'textarea',
    'a',
    'button',
    'object',
    '[tabindex]',
    'audio[controls]',
    'video[controls]',
    '[contenteditable]:not([contenteditable="false"])'
].join(',');
const isHidden = (el)=>{
    return el.offsetWidth <= 0 && el.offsetHeight <= 0 || el.style.display === 'none' || el.style.visibility === 'hidden' || el.style.opacity === '0';
};
const isVisible = (element, parentContainer)=>{
    let parentElement = element;
    while(parentElement){
        if (parentElement === parentContainer || parentElement === document.body) {
            break;
        }
        if (isHidden(parentElement)) {
            return false;
        }
        parentElement = parentElement.parentNode;
    }
    return true;
};
const getElementTabIndex = (element)=>{
    const tabIndex = element.getAttribute('tabindex');
    return tabIndex === null ? NaN : parseInt(tabIndex, 10);
};
const isFocusable = (element, parentContainer)=>{
    const nodeName = element.nodeName.toLowerCase();
    const isTabIndexNotNaN = !Number.isNaN(getElementTabIndex(element));
    const res = tabbableNode.test(nodeName) && !element.disabled || (element instanceof HTMLAnchorElement ? element.href || isTabIndexNotNaN : isTabIndexNotNaN);
    return Boolean(res) && isVisible(element, parentContainer);
};
const isTabble = (element, parentContainer)=>{
    const tabIndex = getElementTabIndex(element);
    const isTabIndexNaN = Number.isNaN(tabIndex);
    return (isTabIndexNaN || tabIndex >= 0) && isFocusable(element, parentContainer);
};
const findTabbableDescendants = (element)=>{
    return Array.from(element.querySelectorAll(focusSelector)).filter((el)=>isTabble(el, element));
};
