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

export function useSwapHandler() {
    const { materialStore } = useSceneContext();
    const { getMaterialById, setMaterial } = materialStore();

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

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

        const { material: newMaterialType } = action;
        const material = getMaterialById(materialId);
        if (!material) return;

        setMaterial(material.materialId, newMaterialType);
        swapLogStatus(material.materialName, `Swapped to ${newMaterialType}`);

    }, [getMaterialById, setMaterial]);

    return {
        handleSwap,
    };
}