import React, { forwardRef, useCallback, useMemo, useState, useRef, useLayoutEffect } from "react";
import { cx, safeUseId } from "../../../../../utils";
import { IconDisclosureLeft, IconDisclosureRight } from "../../../../_Icon";
import { classes } from "../../../tokens";
import { TabItemRefs, TabsContext } from "../../../TabsContext";
import { getFirstOverflowingTab, getLastOverflowingTab } from "../../../utils";
import { base as sizeCSS } from "./variations/_size/base";
import { base as viewCSS } from "./variations/_view/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base as pilledCSS } from "./variations/_pilled/base";
import { base as stretchCSS } from "./variations/_stretch/base";
import { StyledArrow, StyledContent, StyledContentWrapper, base } from "./HorizontalTabs.styles";
var Keys = /*#__PURE__*/ function(Keys) {
    Keys[Keys["end"] = 35] = "end";
    Keys[Keys["home"] = 36] = "home";
    Keys[Keys["left"] = 37] = "left";
    Keys[Keys["right"] = 39] = "right";
    return Keys;
}(Keys || {});
const getIconSize = (size)=>{
    return size === 'h1' || size === 'h2' ? 'm' : 's';
};
// TODO: https://github.com/salute-developers/plasma/issues/1474
export const horizontalTabsRoot = (Root)=>/*#__PURE__*/ forwardRef((props, outerRef)=>{
        const { id, stretch = false, disabled = false, clip = 'scroll', size, view, children, pilled = false, index, className, hasDivider = true, ...rest } = props;
        const [firstItemVisible, setFirstItemVisible] = useState(true);
        const [lastItemVisible, setLastItemVisible] = useState(false);
        // Триггер доп. скролла при появлении левой стрелки, которая отъедает ширину scroll-контейнера
        const [shouldTriggerAdditionalScroll, setShouldTriggerAdditionalScroll] = useState(false);
        const refs = useMemo(()=>new TabItemRefs(index), []);
        const uniqId = safeUseId();
        const tabsId = id || uniqId;
        const isFilled = view === 'filled'; // outer padding is only for filled view
        const pilledAttr = view !== 'clear' && pilled;
        const pilledClass = pilledAttr ? classes.tabsPilled : undefined;
        const noDividerClass = !hasDivider ? classes.tabsNoDivider : undefined;
        const stretchClass = firstItemVisible && lastItemVisible && stretch ? classes.tabsStretch : undefined;
        const hasLeftArrowClass = !firstItemVisible ? classes.tabsHasLeftArrow : undefined;
        const hasRightArrowClass = !lastItemVisible ? classes.tabsHasRightArrow : undefined;
        const clipScrollClass = clip === 'scroll' ? classes.tabsClipScroll : undefined;
        const clipShowAllClass = clip === 'showAll' ? classes.tabsClipShowAll : undefined;
        const scrollRef = useRef(null);
        const trackRef = useRef(null);
        const leftArrowRef = useRef(null);
        const onPrev = useCallback(()=>{
            if (disabled || !scrollRef.current) {
                return;
            }
            const { scrollLeft } = scrollRef.current;
            const firstOverflowingTab = getFirstOverflowingTab(refs.items.slice().reverse(), scrollLeft, rest.orientation);
            if (!firstOverflowingTab?.current) {
                return;
            }
            const scrollElStyle = getComputedStyle(scrollRef.current);
            scrollRef.current.scrollTo({
                left: firstOverflowingTab.current.offsetLeft - parseInt(scrollElStyle.paddingLeft, 10)
            });
        }, [
            disabled,
            scrollRef,
            refs
        ]);
        const onNext = useCallback(()=>{
            if (disabled || !scrollRef.current) {
                return;
            }
            const scrollRight = Math.round(scrollRef.current.scrollLeft + scrollRef.current.clientWidth);
            const lastOverflowingTab = getLastOverflowingTab(refs.items, scrollRight, rest.orientation);
            if (!lastOverflowingTab?.current) {
                return;
            }
            if (firstItemVisible) {
                setShouldTriggerAdditionalScroll(true);
            }
            const scrollElStyle = getComputedStyle(scrollRef.current);
            scrollRef.current.scrollTo({
                left: Math.round(lastOverflowingTab.current.offsetLeft + lastOverflowingTab.current.offsetWidth - scrollRef.current.clientWidth + parseInt(scrollElStyle.paddingRight, 10))
            });
        }, [
            firstItemVisible,
            disabled,
            scrollRef,
            refs
        ]);
        const PreviousButton = useMemo(()=>/*#__PURE__*/ React.createElement(StyledArrow, {
                type: "button",
                "aria-label": "Предыдущий таб",
                onClick: onPrev,
                tabIndex: disabled ? -1 : 0,
                disabled: disabled,
                isFilled: isFilled,
                ref: leftArrowRef,
                isLeftArrow: true
            }, /*#__PURE__*/ React.createElement(IconDisclosureLeft, {
                size: getIconSize(size),
                color: "inherit"
            })), [
            onPrev,
            size,
            disabled,
            isFilled
        ]);
        const NextButton = useMemo(()=>/*#__PURE__*/ React.createElement(StyledArrow, {
                type: "button",
                "aria-label": "Следующий таб",
                onClick: onNext,
                tabIndex: disabled ? -1 : 0,
                disabled: disabled,
                isFilled: isFilled
            }, /*#__PURE__*/ React.createElement(IconDisclosureRight, {
                size: getIconSize(size),
                color: "inherit"
            })), [
            onNext,
            size,
            disabled,
            isFilled
        ]);
        const handleScroll = useCallback((event)=>{
            event.stopPropagation();
            const scrollElStyle = getComputedStyle(event.currentTarget);
            const minScrollLeft = parseInt(scrollElStyle.paddingLeft, 10);
            const maxScrollLeft = event.currentTarget.scrollWidth - event.currentTarget.clientWidth - parseInt(scrollElStyle.paddingRight, 10);
            const scrollLeft = Math.round(event.currentTarget.scrollLeft);
            setFirstItemVisible(scrollLeft <= minScrollLeft);
            setLastItemVisible(scrollLeft >= maxScrollLeft);
        }, [
            setFirstItemVisible,
            setLastItemVisible
        ]);
        const onKeyDown = useCallback((event)=>{
            if (index === undefined) {
                return;
            }
            const minIndex = 0;
            const maxIndex = refs.items.length - 1;
            let nextIndex;
            switch(event.keyCode){
                case 35:
                    nextIndex = maxIndex;
                    break;
                case 37:
                    nextIndex = index > minIndex ? index - 1 : index;
                    break;
                case 39:
                    nextIndex = index < maxIndex ? index + 1 : index;
                    break;
                case 36:
                    nextIndex = minIndex;
                    break;
                default:
                    return;
            }
            if (nextIndex !== index) {
                event.preventDefault();
                refs.items[nextIndex].current?.focus();
                refs.items[nextIndex].current?.scrollIntoView({
                    block: 'nearest',
                    inline: 'center',
                    behavior: 'smooth'
                });
            }
        }, [
            index
        ]);
        useLayoutEffect(()=>{
            setLastItemVisible(scrollRef.current?.scrollWidth === scrollRef.current?.clientWidth);
        }, [
            clip
        ]);
        // Этот хук компенсирует появление левой стрелки при прокрутке
        useLayoutEffect(()=>{
            if (firstItemVisible || !shouldTriggerAdditionalScroll || !scrollRef.current || !leftArrowRef.current) {
                return;
            }
            const style = getComputedStyle(scrollRef.current);
            scrollRef.current.scrollTo({
                left: Math.round(scrollRef.current.scrollLeft + leftArrowRef.current.clientWidth + parseInt(style.paddingLeft, 10))
            });
            setShouldTriggerAdditionalScroll(false);
        }, [
            firstItemVisible,
            shouldTriggerAdditionalScroll,
            scrollRef,
            leftArrowRef
        ]);
        return /*#__PURE__*/ React.createElement(TabsContext.Provider, {
            value: refs
        }, /*#__PURE__*/ React.createElement(Root, {
            view: view,
            role: "tablist",
            // TODO: убрать каст any, когда будут удалены deprecated props
            size: size,
            pilled: pilled,
            id: tabsId,
            ref: outerRef,
            disabled: disabled,
            className: cx(pilledClass, stretchClass, hasLeftArrowClass, hasRightArrowClass, noDividerClass, className),
            onKeyDown: onKeyDown,
            ...rest
        }, !firstItemVisible && clip === 'scroll' && PreviousButton, /*#__PURE__*/ React.createElement(StyledContentWrapper, {
            className: cx(clipScrollClass, clipShowAllClass),
            ref: scrollRef,
            onScroll: handleScroll
        }, /*#__PURE__*/ React.createElement(StyledContent, {
            ref: trackRef
        }, children)), !lastItemVisible && clip === 'scroll' && NextButton));
    });
export const horizontalTabsConfig = {
    name: 'HorizontalTabs',
    tag: 'div',
    layout: horizontalTabsRoot,
    base,
    variations: {
        size: {
            css: sizeCSS
        },
        view: {
            css: viewCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        },
        stretch: {
            css: stretchCSS,
            attrs: true
        },
        pilled: {
            css: pilledCSS
        }
    },
    defaults: {
        view: 'clear',
        size: 'xs'
    }
};
