import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useRef, useEffect } from "react";
import styles from "./style.module.css";
import { MdKeyboardArrowUp, MdKeyboardArrowDown } from "react-icons/md";
import { MAIN_BLUE } from "../styles/colors";
export default function BlockSelector4({ gameRef, staticBlock, staticBlockItem, posterItems, }) {
    const containerRef = useRef(null);
    const scrollContainerRef = useRef(null);
    const staticBlockRef = useRef(null);
    const scrollTimeoutRef = useRef(null);
    const itemHeight = 50;
    // start at the middle item
    useEffect(() => {
        if (scrollContainerRef.current) {
            scrollContainerRef.current.scrollTop = posterItems.length * itemHeight / 2;
        }
    }, [posterItems]);
    const onBlockChange = (item) => {
        if (!gameRef.current || !gameRef.current.scene)
            return;
        switch (staticBlock) {
            case "left":
                gameRef.current.scene.selectedCogTiltLeftBlock = item;
                break;
            case "right":
                gameRef.current.scene.selectedCogTiltRightBlock = item;
                break;
        }
    };
    const getSnappedItem = () => {
        if (!staticBlockRef.current || !scrollContainerRef.current)
            return null;
        const staticRect = staticBlockRef.current.getBoundingClientRect();
        const staticCenterY = staticRect.top + staticRect.height / 2;
        const items = scrollContainerRef.current.querySelectorAll(`.${styles.carouselItem}`);
        let closestEl = null;
        let minDist = Infinity;
        items.forEach(el => {
            const r = el.getBoundingClientRect();
            const dist = Math.abs(staticCenterY - (r.top + r.height / 2));
            if (dist < minDist) {
                minDist = dist;
                closestEl = el;
            }
        });
        if (closestEl) {
            const idx = parseInt(closestEl.getAttribute("data-index") || "", 10);
            return posterItems[idx];
        }
        return null;
    };
    const handleScroll = () => {
        if (scrollTimeoutRef.current) {
            clearTimeout(scrollTimeoutRef.current);
        }
        scrollTimeoutRef.current = setTimeout(() => {
            const snapped = getSnappedItem();
            if (snapped)
                onBlockChange(snapped);
        }, 150);
    };
    const scrollByItem = (dir) => {
        const el = scrollContainerRef.current;
        if (!el)
            return;
        const delta = dir === "up" ? -itemHeight : itemHeight;
        // scrollBy with smooth behavior
        el.scrollBy({ top: delta, behavior: "smooth" });
    };
    return (_jsxs("div", { className: styles.outerContainer, children: [_jsx("div", { onClick: () => scrollByItem("down"), className: styles.upArrowContainer, children: _jsx(MdKeyboardArrowUp, { color: MAIN_BLUE, size: 40 }) }), _jsxs("div", { ref: containerRef, className: styles.carouselBody, children: [_jsx("div", { ref: staticBlockRef, className: styles.staticBlockItem, children: _jsx(staticBlockItem.icon, { className: styles.staticBlockItemIcon }) }), _jsx("div", { ref: scrollContainerRef, className: styles.scrollContainer, onScroll: handleScroll, style: {
                            overflowY: "auto",
                            paddingTop: (itemHeight * 2 - 2),
                            paddingBottom: (itemHeight * 2 - 2),
                        }, children: posterItems.map((item, index) => (_jsx("div", { "data-index": index, className: styles.carouselItem, style: { height: itemHeight }, children: _jsx(item.icon, { className: styles.carouselItemIcon }) }, index + item.block))) })] }), _jsx("div", { onClick: () => scrollByItem("up"), className: styles.downArrowContainer, children: _jsx(MdKeyboardArrowDown, { color: MAIN_BLUE, size: 40 }) })] }));
}
