"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import { useKeyboardMovementProvider } from "../movement/useKeyboardMovementProvider.js";
import { useDir } from "../typography/WritingDirectionProvider.js";
import { useEnsuredRef } from "../useEnsuredRef.js";
import { useResizeObserver } from "../useResizeObserver.js";
import { getTabRoleOnly, scrollTabIntoView } from "./utils.js";
const TAB_SIZE_VAR = "--rmd-tab-size";
const TAB_OFFSET_VAR = "--rmd-tab-offset";
const noop = ()=>{
// do nothing
};
/**
 * @internal
 */ export function useTabList(options) {
    const { ref: propRef, style, activeIndex, scrollButtons, onClick = noop, onFocus, onKeyDown, activationMode, vertical, setActiveIndex, disableTransition } = options;
    const isRTL = useDir().dir === "rtl";
    const [indicatorStyles, setIndicatorStyles] = useState(()=>{
        const tabWidth = `${100 / 3}%`;
        return {
            [TAB_SIZE_VAR]: tabWidth,
            [TAB_OFFSET_VAR]: "0px"
        };
    });
    const [nodeRef, ref] = useEnsuredRef(propRef);
    const tabListRef = useResizeObserver({
        ref,
        disabled: disableTransition,
        onUpdate: useCallback((entry)=>{
            // this is kind of hacky -- the styles should update when switching between
            // RTL, but the RTL state isn't required for any styles. Just reference it
            // so that the hooks eslint rule doesn't show a warning...
            isRTL;
            const activeTab = getTabRoleOnly(entry.target)[activeIndex];
            if (!activeTab) {
                return;
            }
            const size = vertical ? activeTab.offsetHeight : activeTab.offsetWidth;
            const offset = vertical ? activeTab.offsetTop : activeTab.offsetLeft;
            const cssVars = {
                [TAB_SIZE_VAR]: `${size}px`,
                [TAB_OFFSET_VAR]: `${offset}px`
            };
            setIndicatorStyles((prevStyles)=>{
                if (prevStyles && prevStyles[TAB_SIZE_VAR] === cssVars[TAB_SIZE_VAR] && prevStyles[TAB_OFFSET_VAR] === cssVars[TAB_OFFSET_VAR]) {
                    return prevStyles;
                }
                return cssVars;
            });
        }, [
            activeIndex,
            isRTL,
            vertical
        ])
    });
    const forwardRef = useRef(null);
    const backwardRef = useRef(null);
    const { movementProps, movementContext } = useKeyboardMovementProvider({
        onClick (event) {
            onClick(event);
            if (event.isPropagationStopped() || !(event.target instanceof Element)) {
                return;
            }
            const clickedTab = event.target.closest("[role='tab']");
            const tabs = getTabRoleOnly(event.currentTarget);
            const i = tabs.findIndex((tab)=>tab === clickedTab);
            if (i !== -1) {
                setActiveIndex(i);
            }
        },
        onFocus,
        onKeyDown,
        onFocusChange (event) {
            const { index } = event;
            if (activationMode === "automatic") {
                setActiveIndex(index);
            } else if (scrollButtons) {
                scrollTabIntoView({
                    activeIndex: index,
                    backward: backwardRef.current,
                    container: nodeRef.current,
                    forward: forwardRef.current,
                    vertical
                });
            }
        },
        loopable: true,
        searchable: true,
        horizontal: !vertical,
        includeDisabled: true,
        tabIndexBehavior: "roving",
        getFocusableElements: getTabRoleOnly
    });
    useEffect(()=>{
        scrollTabIntoView({
            activeIndex,
            backward: backwardRef.current,
            container: nodeRef.current,
            forward: forwardRef.current,
            vertical
        });
    }, [
        activeIndex,
        nodeRef,
        vertical
    ]);
    return {
        elementProps: {
            "aria-orientation": vertical ? "vertical" : "horizontal",
            ref: tabListRef,
            style: {
                ...style,
                ...disableTransition ? undefined : indicatorStyles
            },
            ...movementProps
        },
        backwardProps: {
            ref: backwardRef,
            type: "back",
            vertical
        },
        forwardProps: {
            ref: forwardRef,
            type: "forward",
            vertical
        },
        movementContext
    };
}

//# sourceMappingURL=useTabList.js.map