"use client";
import { useEffect, useRef, useState } from "react";
import { useEnsuredId } from "../useEnsuredId.js";
import { useEnsuredState } from "../useEnsuredState.js";
import { applyRef } from "../utils/applyRef.js";
const EMPTY_LIST = [];
const PANEL_PREFIX = "panel-";
/**
 * This hook can be uncontrolled/controlled and supports strongly typing the tab
 * values if needed. Check out the overloads for examples.
 *
 * @see {@link https://react-md.dev/components/tabs | Tabs Demos}
 * @since 6.0.0
 */ export function useTabs(options = {}) {
    const { baseId: propBaseId, tabs = EMPTY_LIST, stacked, vertical, iconAfter, activeTab: propActiveTab, setActiveTab: propSetActiveTab, defaultActiveTab, disableScrollFix, disableTransition } = options;
    const baseId = useEnsuredId(propBaseId, "tab");
    const [direction, setDirection] = useState("forward");
    const [activeTab, setActiveTab] = useEnsuredState({
        name: "activeTab",
        value: propActiveTab,
        setValue: propSetActiveTab,
        defaultValue: defaultActiveTab ?? 0
    });
    const getTabIndex = (tabValue)=>typeof tabValue === "string" || tabs.length > 0 ? tabs.indexOf(tabValue) : tabValue;
    const getTabId = (tabValue, prefix = "")=>`${baseId}-${prefix}${getTabIndex(tabValue) + 1}`;
    const activeIndex = getTabIndex(activeTab);
    const tabPanelsRef = useRef(null);
    useEffect(()=>{
        const container = tabPanelsRef.current;
        if (!container || disableScrollFix) {
            return;
        }
        container.scrollTop = 0;
    }, [
        disableScrollFix,
        activeTab
    ]);
    return {
        activeTab,
        setActiveTab,
        direction,
        setDirection,
        getTabProps (tabValue) {
            return {
                "aria-controls": getTabId(tabValue, PANEL_PREFIX),
                id: getTabId(tabValue),
                active: tabValue === activeTab,
                stacked,
                iconAfter,
                activeIndicator: disableTransition,
                verticalActiveIndicator: vertical && disableTransition
            };
        },
        getTabListProps () {
            return {
                activeIndex,
                setActiveIndex: (nextActiveIndex)=>{
                    setDirection(activeIndex < nextActiveIndex ? "forward" : "backward");
                    if (typeof activeTab === "string" || tabs.length > 0) {
                        setActiveTab(tabs[nextActiveIndex]);
                    } else {
                        setActiveTab(nextActiveIndex);
                    }
                },
                vertical,
                disableTransition
            };
        },
        getTabPanelProps (tabValue) {
            return {
                "aria-labelledby": getTabId(tabValue),
                id: getTabId(tabValue, PANEL_PREFIX),
                role: "tabpanel",
                active: tabValue === activeTab
            };
        },
        getTabPanelsProps (ref) {
            return {
                ref: (instance)=>{
                    applyRef(instance, ref);
                    applyRef(instance, tabPanelsRef);
                },
                direction
            };
        }
    };
}

//# sourceMappingURL=useTabs.js.map