import { useEffect } from 'react'
import { useMaterialStore } from '../../../../../store/simulation/useMaterialStore';
import { useConveyorStore } from '../../../../../store/simulation/useConveyorStore';
import { useResetButtonStore } from '../../../../../store/usePlayButtonStore';
import { useSelectedProduct } from '../../../../../store/simulation/useSimulationStore';
import { useProductStore } from '../../../../../store/simulation/useProductStore';
// import { findConveyorSubsequence } from '../../../simulator/functions/getConveyorSequencesInProduct';

function ConveyorInstance({ conveyor }: { conveyor: ConveyorStatus }) {
    const { getProductById } = useProductStore();
    const { selectedProduct } = useSelectedProduct();
    const { materials, getMaterialsByCurrentModelUuid } = useMaterialStore();
    const { isReset } = useResetButtonStore();
    const { setConveyorPaused } = useConveyorStore();

    useEffect(() => {
        const product = getProductById(selectedProduct.productId);
        if (!product) return;

        const conveyorMaterials = getMaterialsByCurrentModelUuid(conveyor.modelUuid);
        if (conveyorMaterials && conveyorMaterials?.length > 0) {

            const hasPausedMaterials = conveyorMaterials.some(material => material.isPaused);

            if (hasPausedMaterials) {
                setConveyorPaused(conveyor.modelUuid, true);
            } else {
                setConveyorPaused(conveyor.modelUuid, false);
            }
        } else {
            setConveyorPaused(conveyor.modelUuid, false);
        }

        // const conveyorSubsequence = findConveyorSubsequence(product, conveyor.modelUuid);

        // if (!conveyorSubsequence || !conveyorSubsequence.currentSubSequence) {
        //     setConveyorPaused(conveyor.modelUuid, false);
        //     return;
        // }

        // const { currentSubSequence } = conveyorSubsequence;

        // const allMaterials = currentSubSequence.flatMap(event =>
        //     getMaterialsByCurrentModelUuid(event.modelUuid)
        // );

        // const hasPausedMaterials = allMaterials.some(mat => mat?.isPaused);

        // currentSubSequence.forEach(event => {
        //     if (event.type === 'transfer') {
        //         setConveyorPaused(event.modelUuid, hasPausedMaterials);
        //     }
        // });

    }, [materials, conveyor.modelUuid, getMaterialsByCurrentModelUuid, setConveyorPaused, isReset, selectedProduct.productId, getProductById]);

    useEffect(() => {
        // console.log('conveyor: ', conveyor);
    }, [conveyor])

    return (
        <>
        </>
    )
};

export default ConveyorInstance