import hasProp from './hasProp';
import getActiveElement from './getActiveElement';
import createUID from './createUID';
export const focusableSelector = 'a[href], button, input, textarea, select, details, video[controls], audio[controls], [tabindex]:not([tabindex="-1"])';
/**
 * Returns all valid focusable elements within a given ref.
 * @param container The container element, or reference to the container element, to search for focusable elements within.
 * @param options
 * @param options.includeActiveEl If true, the active element will be included in the list, even if it does not match focusable criteria.
 * @returns A list of valid focusable elements within the given ref.
 */
const getFocusables = (container, { includeActiveEl = false } = {}) => {
    const uid = createUID();
    let activeId = uid;
    let activeEl = null;
    const containerEl = hasProp(container, 'current') ? container.current : container;
    if (!containerEl)
        return [];
    let selector = focusableSelector;
    if (includeActiveEl) {
        activeEl = getActiveElement();
        if (activeEl) {
            activeId = activeEl.id || uid;
            activeEl.id = activeId;
            selector = `${selector}, [id="${activeId}"]`;
        }
    }
    const focusableElements = [...containerEl.querySelectorAll(selector)].filter(el => el instanceof HTMLElement &&
        (el.id === activeId ||
            (!el.hasAttribute('disabled') &&
                el.getAttribute('tabindex') !== '-1' &&
                !el.closest('[inert]'))));
    if (activeEl?.id === uid) {
        activeEl.removeAttribute('id');
    }
    return focusableElements;
};
export default getFocusables;
//# sourceMappingURL=getFocusables.js.map