import hasProp from './hasProp';
import getActiveElement from './getActiveElement';
import createUID from './createUID';
import isInstance from './isInstance';
export const focusableSelector = 'a[href], button, input, textarea, select, details, video[controls], audio[controls], [tabindex]';
/**
 * 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.
 * @param options.includeNegativeTabIndex If true, elements with negative tabindex will be included in the list.
 * @returns A list of valid focusable elements within the given ref.
 */
const getFocusables = (container, { includeActiveEl = false, includeNegativeTabIndex = 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 => {
        const passesTabIndex = includeNegativeTabIndex || el.getAttribute('tabindex') !== '-1';
        return (isInstance(el, Element) &&
            (el.id === activeId ||
                (!el.hasAttribute('disabled') && !el.closest('[inert]') && passesTabIndex)));
    });
    if (activeEl?.id === uid) {
        activeEl.removeAttribute('id');
    }
    return focusableElements;
};
export default getFocusables;
//# sourceMappingURL=getFocusables.js.map