import {createSlice, PayloadAction} from "@reduxjs/toolkit";

export interface ScanModalState {
    isVisible: boolean;
    scannedData?: string;
    customLabel?: string;
    data?: any;
    uuid?: string;
}

const initialState: ScanModalState = {
    isVisible: false,
}

export const scanModalSlice = createSlice({
    name: 'scanModal',
    initialState,
    reducers: {
        openScanModalFormContext(state, action: PayloadAction<{ uuid: string }>) {
            state.isVisible = true;
            state.uuid = action.payload.uuid;
        },
        openScanModal: (state, action: PayloadAction<{ customLabel?: string, data?: any }>) => {
            state.isVisible = true
            if (!action) return;
            state.customLabel = action.payload.customLabel;
            state.data = action.payload.data;
        },
        closeScanModal: (state) => {
            state.isVisible = false
            state.customLabel = undefined;
            state.data = undefined;
        },
        setScannedData: (state, action) => {
            state.scannedData = action.payload;
        },
        resolveUuid: (state) => {
            state.uuid = undefined;
        }
    }
})

export const {openScanModal, closeScanModal, setScannedData, openScanModalFormContext,resolveUuid} = scanModalSlice.actions;
export default scanModalSlice.reducer;