'use client';
import { useARIAButtonProps } from '@fluentui/react-aria';
import { useTabsterAttributes } from '@fluentui/react-tabster';
import { getIntrinsicElementProps, isHTMLElement, slot, useEventCallback, useIsomorphicLayoutEffect, useMergedRefs } from '@fluentui/react-utilities';
import * as React from 'react';
import { useCarouselContext_unstable as useCarouselContext } from '../CarouselContext';
import { useCarouselNavContext } from '../CarouselNav/CarouselNavContext';
import { useCarouselNavIndexContext } from '../CarouselNav/CarouselNavIndexContext';
/**
 * Create the state required to render CarouselNavButton.
 *
 * The returned state can be modified with hooks such as useCarouselNavButtonStyles_unstable,
 * before being passed to renderCarouselNavButton_unstable.
 *
 * @param props - props from this instance of CarouselNavButton
 * @param ref - reference to root HTMLDivElement of CarouselNavButton
 */ export const useCarouselNavButton_unstable = (props, ref)=>{
    const { onClick, as = 'button' } = props;
    const { appearance } = useCarouselNavContext();
    const index = useCarouselNavIndexContext();
    const selectPageByIndex = useCarouselContext((ctx)=>ctx.selectPageByIndex);
    const selected = useCarouselContext((ctx)=>ctx.activeIndex === index);
    const subscribeForValues = useCarouselContext((ctx)=>ctx.subscribeForValues);
    const resetAutoplay = useCarouselContext((ctx)=>ctx.resetAutoplay);
    const handleClick = useEventCallback((event)=>{
        onClick === null || onClick === void 0 ? void 0 : onClick(event);
        if (!event.defaultPrevented && isHTMLElement(event.target)) {
            selectPageByIndex(event, index);
        }
        // Ensure any autoplay timers are extended/reset
        resetAutoplay();
    });
    const defaultTabProps = useTabsterAttributes({
        focusable: {
            isDefault: selected
        }
    });
    const buttonRef = React.useRef(undefined);
    const _carouselButton = slot.always(getIntrinsicElementProps(as, useARIAButtonProps(props.as, props)), {
        elementType: 'button',
        defaultProps: {
            ref: useMergedRefs(ref, buttonRef),
            role: 'tab',
            type: 'button',
            'aria-selected': selected,
            ...defaultTabProps
        }
    });
    useIsomorphicLayoutEffect(()=>{
        return subscribeForValues((data)=>{
            var _data_groupIndexList;
            var _data_groupIndexList_index;
            const controlList = (_data_groupIndexList_index = (_data_groupIndexList = data.groupIndexList) === null || _data_groupIndexList === void 0 ? void 0 : _data_groupIndexList[index]) !== null && _data_groupIndexList_index !== void 0 ? _data_groupIndexList_index : [];
            const _controlledSlideIds = controlList.map((slideIndex)=>{
                return data.slideNodes[slideIndex].id;
            }).join(' ');
            if (buttonRef.current) {
                buttonRef.current.setAttribute('aria-controls', _controlledSlideIds);
            }
        });
    }, [
        index,
        subscribeForValues
    ]);
    // Override onClick
    _carouselButton.onClick = handleClick;
    const state = {
        selected,
        appearance,
        components: {
            root: 'button'
        },
        root: _carouselButton
    };
    return state;
};
