import { useCallback } from "react";
import { useSceneContext } from "../../../../scene/sceneContext";

export function useDespawnHandler() {
    const { materialStore } = useSceneContext();
    const { getMaterialById, removeMaterial, setEndTime, clearLocations } = materialStore();

    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);
        clearLocations(material.materialId);

        deSpawnLogStatus(material.materialName, `Despawned`);

    }, [getMaterialById, removeMaterial]);

    return {
        handleDespawn,
    };
}