const queries = [
    'input:not([type="hidden"]):not([disabled])',
    "button:not([disabled])",
    "textarea:not([disabled])",
    "select:not([disabled])",
    "a[href]",
    "area[href]",
    "[tabindex]"
];
/**
 * A query selector to find elements that are programmatically focusable.
 *
 * Example:
 *
 * ```ts
 * const focusableElements = document.querySelectorAll(PROGRAMMATICALLY_FOCUSABLE);
 * // do something with elements
 * ```
 */ export const PROGRAMMATICALLY_FOCUSABLE = queries.join(",");
/**
 * A query selector to find elements that are focusable only with tab and shift+tab.
 *
 * Example:
 *
 * ```ts
 * const focusableElements = document.querySelectorAll(TAB_FOCUSABLE);
 * // do something with elements
 * ```
 *
 * @since 6.0.0 This was updated to remove ALL elements that have a
 * `tabindex="-1"` applied instead of only elements that had a manual tab index
 * applied.
 */ export const TAB_FOCUSABLE = queries.reduce((fullQuery, query)=>{
    const prefix = `${fullQuery}${fullQuery ? "," : ""}`;
    const notProgrammaticQuery = `${query}:not([tabindex="-1"])`;
    return `${prefix}${notProgrammaticQuery}`;
}, "");
/**
 * A simple util that will find all the tab focusable elements within a
 * container element.  The container should normally be a specific HTMLElement,
 * but it can also be the entire document if you want to find **all** focusable
 * elements within your page.
 *
 * @since 6.0.0 This function will be used for all keyboard focus behavior
 * instead of trying to use refs. I did some performance testing in Firefox and
 * Chrome and the `querySelectorAll` takes 4ms with 5000 elements for my use
 * cases. There is more of a performance issue around rendering all 5000
 * elements and since this is only used for specific keydown events, this is
 * good enough for me.
 *
 * @param container - The container element/document to find focusable elements
 * within.
 * @param programmatic - Boolean if programmatically focusable elements should be
 * included instead of only tab focusable.
 * @returns A list of HTMLElements that are focusable within the container.
 */ export function getFocusableElements(container, programmatic = false) {
    // spread operator is faster than Array.from
    return [
        ...container.querySelectorAll(programmatic ? PROGRAMMATICALLY_FOCUSABLE : TAB_FOCUSABLE)
    ];
}
/**
 * Attempts to find the first focusable element within a container.
 *
 * @since 6.0.0
 * @param container - The container element/document to find focusable elements
 * within.
 * @param programmatic - Boolean if programmatically focusable elements should be
 * included instead of only tab focusable.
 * @returns A list of HTMLElements that are focusable within the container.
 */ export function getFocusableElement(container, programmatic = false) {
    return container.querySelector(programmatic ? PROGRAMMATICALLY_FOCUSABLE : TAB_FOCUSABLE);
}
export function isFocusable(element, type = "programmatic") {
    const selector = type === "tab" ? TAB_FOCUSABLE : PROGRAMMATICALLY_FOCUSABLE;
    return "matches" in element && element.matches(selector);
}
export function focusElementWithin(options) {
    const { type, query = "", container, elements } = options;
    if (type === "none") {
        return;
    }
    let element = null;
    if (type === "first") {
        [element] = elements;
    } else if (type === "last") {
        element = elements[elements.length - 1];
    } else if (type === "query") {
        element = document.querySelector(query);
    }
    if (!element && isFocusable(container)) {
        element = container;
    }
    // TODO: Why did I add preventScroll?
    element?.focus();
}

//# sourceMappingURL=utils.js.map