// (C) 2025 GoodData Corporation
import { useCallback, useEffect, useRef } from "react";
import { createContextStore } from "@gooddata/sdk-ui";
import { useIdPrefixed } from "../../utils/useId.js";
/**
 * @internal
 */
export function useFocusWithinContainer(idToFocus) {
    const containerRef = useRef(null);
    useEffect(() => {
        const container = containerRef.current;
        const elementToFocus = idToFocus ? container?.querySelector(`#${idToFocus}`) : null;
        if (!elementToFocus) {
            return;
        }
        if (!container?.contains(document.activeElement) && container !== document.activeElement) {
            return;
        }
        elementToFocus.focus();
    }, [idToFocus]);
    return { containerRef };
}
/**
 * @internal
 */
export const useListWithActionsFocusStoreValue = (getIdFromItem) => {
    const containerId = useIdPrefixed("focus-store");
    const makeId = useCallback(({ item, action }) => CSS.escape(`${containerId}-${getIdFromItem(item)}-${action}`.replace(/\s/g, "_")), [containerId, getIdFromItem]);
    return { makeId, containerId };
};
/**
 * @internal
 */
export const ListWithActionsFocusStore = createContextStore("ListWithActionsFocusStore");
//# sourceMappingURL=useListWithActionsFocus.js.map