"use client";
import { useEffect, useRef, useState } from "react";
import { useEnsuredId } from "../useEnsuredId.js";
import { useEnsuredState } from "../useEnsuredState.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.
 *
 * @remarks \@since 6.0.0
 */ export function useTabs(options = {}) {
    const { baseId: propBaseId, disableScrollFix, tabs = EMPTY_LIST, activeTab: propActiveTab, setActiveTab: propSetActiveTab, defaultActiveTab } = options;
    const baseId = useEnsuredId(propBaseId, "tab");
    const [direction, setDirection] = useState("left");
    const [activeTab, setActiveTab] = useEnsuredState({
        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
            };
        },
        getTabListProps () {
            return {
                activeIndex,
                setActiveIndex: (nextActiveIndex)=>{
                    setDirection(activeIndex < nextActiveIndex ? "left" : "right");
                    if (typeof activeTab === "string" || tabs.length > 0) {
                        setActiveTab(tabs[nextActiveIndex]);
                    } else {
                        setActiveTab(nextActiveIndex);
                    }
                }
            };
        },
        getTabPanelProps (tabValue) {
            return {
                "aria-labelledby": getTabId(tabValue),
                id: getTabId(tabValue, PANEL_PREFIX),
                role: "tabpanel",
                active: tabValue === activeTab
            };
        },
        getTabPanelsProps () {
            return {
                ref: tabPanelsRef,
                direction
            };
        }
    };
}

//# sourceMappingURL=useTabs.js.map