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

export interface LoadingModalState {
    isVisible: boolean;
    loadingText: string;
    indeterminate: boolean;
    progress: number;
}

const initialState: LoadingModalState = {
    isVisible: false,
    loadingText: "Подождите...",
    indeterminate: true,
    progress: 0
}

export const loadingModalSlice = createSlice({
    name: 'loadingModal',
    initialState,
    reducers: {
        openLoadingModal: (state) => {
            state.isVisible = true
        },
        closeLoadingModal: (state) => {
            state.isVisible = false
        },
        setLoadingText: (state, action) => {
            state.loadingText = action.payload;
        },
        setProgress: (state, action: PayloadAction<number>) => {
            state.progress = action.payload;
        },
        setIndeterminate: (state, action: PayloadAction<boolean>) => {
            state.indeterminate = action.payload;
        },
        reset: (state) => {
            state = initialState;
        }
    }
})

export const {
    openLoadingModal,
    closeLoadingModal,
    setLoadingText,
    setProgress,
    setIndeterminate
} = loadingModalSlice.actions;
export default loadingModalSlice.reducer;