"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useTabItem", {
    enumerable: true,
    get: function() {
        return useTabItem;
    }
});
const _react = require("react");
const useTabItem = ({ refs, innerRef, itemIndex, selected, orientation, disabled, onIndexChange, onClick })=>{
    const hasKeyNavigation = itemIndex !== undefined && onIndexChange !== undefined;
    const navigationTabIndex = !disabled && refs?.current === itemIndex ? 0 : -1;
    const onItemFocus = (0, _react.useCallback)((event)=>{
        if (disabled) {
            return;
        }
        if (!hasKeyNavigation && innerRef?.current) {
            if (orientation === ' vertical') {
                innerRef.current.scrollTo({
                    top: innerRef.current.offsetTop,
                    behavior: 'smooth'
                });
                return;
            }
            innerRef.current.scrollTo({
                top: 0,
                left: innerRef.current.offsetLeft,
                behavior: 'smooth'
            });
            return;
        }
        if (!refs) {
            return;
        }
        const focusIndex = refs.items.findIndex((itemRef)=>itemRef.current === event.target);
        if (focusIndex === refs.current) {
            return;
        }
        onIndexChange?.(focusIndex);
        refs.setCurrent(focusIndex);
    }, [
        refs,
        innerRef,
        onIndexChange,
        disabled
    ]);
    const handleClick = (event)=>{
        if (!onClick) {
            return;
        }
        onClick(event);
    };
    (0, _react.useEffect)(()=>{
        if (!refs) {
            return;
        }
        refs.register(innerRef);
        return ()=>refs.unregister(innerRef);
    }, [
        refs
    ]);
    (0, _react.useLayoutEffect)(()=>{
        if (!selected || !innerRef.current) {
            return;
        }
        const scrollEl = innerRef.current.parentElement?.parentElement;
        if (!scrollEl) {
            return;
        }
        const scrollElStyle = getComputedStyle(scrollEl);
        if (orientation === ' vertical') {
            scrollEl.scrollTo({
                top: innerRef.current.offsetTop - parseInt(scrollElStyle.paddingTop, 10)
            });
            return;
        }
        scrollEl.scrollTo({
            left: innerRef.current.offsetLeft - parseInt(scrollElStyle.paddingLeft, 10)
        });
    }, [
        selected,
        orientation
    ]);
    return {
        hasKeyNavigation,
        navigationTabIndex,
        onItemFocus,
        handleClick
    };
};
