import { useCallback } from "react";
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";

export function useDespawnHandler() {
    const { getMaterialById, removeMaterial, setEndTime } = useMaterialStore();

    const deSpawnLogStatus = (materialUuid: string, status: string) => {
        echo.info(`${materialUuid}, ${status}`);
    }

    const handleDespawn = useCallback((action: ConveyorAction, materialId?: string) => {
        if (!action || action.actionType !== 'despawn' || !materialId) return;

        const material = getMaterialById(materialId);
        if (!material) return;

        setEndTime(material.materialId, performance.now());
        removeMaterial(material.materialId);

        deSpawnLogStatus(material.materialName, `Despawned`);

    }, [getMaterialById, removeMaterial]);

    return {
        handleDespawn,
    };
}