import {useDispatch, useSelector} from "react-redux";
import {RootState} from "../redux/store";
import {useEffect, useState} from "react";
import {openScanModal} from "../features/scanModal/scanModalSlice";

type ScanProps<T> = {
    label: string;
    onScanned: (scanned: string, data: T) => void;
}
type ReturnValue<T> = {
    scan: (props: ScanProps<T>) => void;
}

function useScan<T>(): ReturnValue<T> {
    const state = useSelector((state: RootState) => state.scanModal);
    const dispatch = useDispatch();
    const scan = (props: ScanProps<T>) => {
        dispatch(openScanModal({customLabel: props.label, data: props}));
    }
    useEffect(() => {

    }, [state.scannedData]);
    return {scan}
}