"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useTabItem", {
    enumerable: true,
    get: function() {
        return useTabItem;
    }
});
var _react = require("react");
var useTabItem = function useTabItem(param) {
    var refs = param.refs, innerRef = param.innerRef, itemIndex = param.itemIndex, selected = param.selected, orientation = param.orientation, disabled = param.disabled, onIndexChange = param.onIndexChange, onClick = param.onClick;
    var hasKeyNavigation = itemIndex !== undefined && onIndexChange !== undefined;
    var navigationTabIndex = !disabled && (refs === null || refs === void 0 ? void 0 : refs.current) === itemIndex ? 0 : -1;
    var onItemFocus = (0, _react.useCallback)(function(event) {
        if (disabled) {
            return;
        }
        if (!hasKeyNavigation && (innerRef === null || innerRef === void 0 ? void 0 : 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;
        }
        var focusIndex = refs.items.findIndex(function(itemRef) {
            return itemRef.current === event.target;
        });
        if (focusIndex === refs.current) {
            return;
        }
        onIndexChange === null || onIndexChange === void 0 ? void 0 : onIndexChange(focusIndex);
        refs.setCurrent(focusIndex);
    }, [
        refs,
        innerRef,
        onIndexChange,
        disabled
    ]);
    var handleClick = function handleClick(event) {
        if (!onClick) {
            return;
        }
        onClick(event);
    };
    (0, _react.useEffect)(function() {
        if (!refs) {
            return;
        }
        refs.register(innerRef);
        return function() {
            return refs.unregister(innerRef);
        };
    }, [
        refs
    ]);
    (0, _react.useLayoutEffect)(function() {
        var _innerRef_current_parentElement;
        if (!selected || !innerRef.current) {
            return;
        }
        var scrollEl = (_innerRef_current_parentElement = innerRef.current.parentElement) === null || _innerRef_current_parentElement === void 0 ? void 0 : _innerRef_current_parentElement.parentElement;
        if (!scrollEl) {
            return;
        }
        var 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: hasKeyNavigation,
        navigationTabIndex: navigationTabIndex,
        onItemFocus: onItemFocus,
        handleClick: handleClick
    };
};
