// (C) 2025-2026 GoodData Corporation
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { DefaultUiTabsAllTabs } from "./defaultComponents/DefaultUiTabsAllTabs.js";
import { DefaultUiTabsAllTabsButton } from "./defaultComponents/DefaultUiTabsAllTabsButton.js";
import { DefaultUiTabsContainer } from "./defaultComponents/DefaultUiTabsContainer.js";
import { DefaultUiTabsTab } from "./defaultComponents/DefaultUiTabsTab.js";
import { DefaultUiTabsTabActions } from "./defaultComponents/DefaultUiTabsTabActions.js";
import { DefaultUiTabsTabActionsButton } from "./defaultComponents/DefaultUiTabsTabActionsButton.js";
import { DefaultUiTabsTabValue } from "./defaultComponents/DefaultUiTabsTabValue.js";
let actionIdCounter = 0;
function createActionListener(actionEventListeners) {
    return (callback) => {
        useEffect(() => {
            const id = actionIdCounter++;
            actionEventListeners.set(id, callback);
            return () => {
                actionEventListeners.delete(id);
            };
        }, [callback]);
    };
}
/**
 * @internal
 */
export function useUiTabsContextStoreValue({ tabs, selectedTabId, onTabSelect, onTabDoubleClick, size = "medium", maxLabelLength, accessibilityConfig: accessibilityConfigProp, disableBottomBorder, Container = DefaultUiTabsContainer, Tab = DefaultUiTabsTab, TabValue = DefaultUiTabsTabValue, TabActions = DefaultUiTabsTabActions, TabActionsButton = DefaultUiTabsTabActionsButton, AllTabs = DefaultUiTabsAllTabs, AllTabsButton = DefaultUiTabsAllTabsButton, }) {
    const [isOverflowing, setIsOverflowing] = useState(false);
    const resizeObserverRef = useRef(null);
    const mutationObserverRef = useRef(null);
    const containerRef = useRef(null);
    const accessibilityConfig = useMemo(() => {
        return { tabRole: "tab", role: "tablist", ...accessibilityConfigProp };
    }, [accessibilityConfigProp]);
    // Check if tabs overflow the container
    const containerRefCallback = useCallback((container) => {
        containerRef.current = container;
        resizeObserverRef.current?.disconnect();
        mutationObserverRef.current?.disconnect();
        if (!container) {
            return undefined;
        }
        const checkOverflow = () => {
            const hasOverflow = container.scrollWidth > container.clientWidth;
            setIsOverflowing(hasOverflow);
        };
        checkOverflow();
        resizeObserverRef.current = new ResizeObserver(checkOverflow);
        resizeObserverRef.current.observe(container);
        mutationObserverRef.current = new MutationObserver(checkOverflow);
        mutationObserverRef.current.observe(container, { childList: true });
    }, []);
    const [actionEventListeners] = useState(new Map());
    const onActionTriggered = useCallback((context) => {
        actionEventListeners.forEach((listener) => listener(context));
    }, [actionEventListeners]);
    const useActionListener = useMemo(() => createActionListener(actionEventListeners), [actionEventListeners]);
    return useMemo(() => ({
        tabs,
        selectedTabId,
        onTabSelect,
        onTabDoubleClick,
        size,
        maxLabelLength,
        disableBottomBorder,
        accessibilityConfig,
        onActionTriggered,
        useActionListener,
        isOverflowing,
        containerRef: containerRefCallback,
        Container,
        Tab,
        TabValue,
        TabActions,
        TabActionsButton,
        AllTabs,
        AllTabsButton,
    }), [
        tabs,
        selectedTabId,
        onTabSelect,
        onTabDoubleClick,
        size,
        maxLabelLength,
        disableBottomBorder,
        accessibilityConfig,
        onActionTriggered,
        useActionListener,
        isOverflowing,
        containerRefCallback,
        Container,
        Tab,
        TabValue,
        TabActions,
        TabActionsButton,
        AllTabs,
        AllTabsButton,
    ]);
}
