import { useCallback, useContext } from 'react';

import { SupplyChainContext } from '@/app/lib/context/SupplyChain/SupplyChainContext';
import { Processor } from '@/models/processor';

export const useGetProcessorByBarcodeId = () => {
    const { processors } = useContext(SupplyChainContext);

    return useCallback((barcodeId?: string) => {
        if (!barcodeId) return null;
        const processor = processors?.find((processor: Processor) => processor.barcode_id === barcodeId);
        return processor as Processor & { _id: string } || null;
    }, [processors]);
};

export const useGetProcessorIdByBarcodeId = () => {
    const getProcessorByBarcodeId = useGetProcessorByBarcodeId();

    return useCallback((barcodeId?: string) => {
        if (!barcodeId) return null;
        return getProcessorByBarcodeId(barcodeId)?._id || null;
    }, [useGetProcessorByBarcodeId]);
};
