import { useEffect, useCallback } from 'react';
import { useStoreHandler } from './actionHandler/useStoreHandler';
import { useRetrieveHandler } from './actionHandler/useRetrieveHandler';

export function useStorageActions() {
    const { handleStore } = useStoreHandler();
    const { handleRetrieve } = useRetrieveHandler();

    const handleStoreAction = useCallback((action: StorageAction, materialId: string) => {
        handleStore(action, materialId);
    }, [handleStore]);

    const handleRetrieveAction = useCallback((action: StorageAction) => {
        handleRetrieve(action);
    }, [handleRetrieve]);

    const handleStorageAction = useCallback((action: StorageAction, materialId: string) => {
        if (!action) return;

        switch (action.actionType) {
            case 'store':
                handleStoreAction(action, materialId);
                break;
            case 'retrieve':
                handleRetrieveAction(action);
                break;
            default:
                console.warn(`Unknown storage action type: ${action.actionType}`);
        }
    }, [handleStoreAction, handleRetrieveAction]);

    const cleanup = useCallback(() => {
    }, []);

    useEffect(() => {
        return () => {
            cleanup();
        };
    }, [cleanup]);

    return {
        handleStorageAction,
        cleanup
    };
}