import {createSlice} from "@reduxjs/toolkit";
import {getArchiveSchedule, insertArchiveSchedule} from "../httpServices/httpServiceArchiveSchedule";

const initialState = {
    archiveSchedule:{},
    loading: true,
    error: null,
}

const archiveScheduleSlice = createSlice({
    name: "archiveSchedule",
    initialState,
    reducers:{
        resetArchiveLoading:(state) =>{
            state.loading = true
        },
        setLoadingTrue: (state) => {
            state.archiveSchedule = {}
            state.loading = true
            state.error = null
        }
    },
    extraReducers:(builder)=>{
        // Add Archive Schedule Settings
        builder.addCase(insertArchiveSchedule.pending, (state, action) => {
            state.loading   = true;
            state.error     = null;
        });
        builder.addCase(insertArchiveSchedule.fulfilled, (state, action) => {
            state.loading   = false;
            state.archiveSchedule  = action.payload;
        });
        builder.addCase(insertArchiveSchedule.rejected, (state,action) => {
            state.loading   = false;
            state.error     = action.payload;
        });

        // Get Archive Schedule Settings
        builder.addCase(getArchiveSchedule.pending, (state) => {
            state.loading   = true;
            state.error     = null;
        })
        builder.addCase(getArchiveSchedule.fulfilled, (state, action) => {
            state.loading   = false;
            state.archiveSchedule  = action.payload;
        })
        builder.addCase(getArchiveSchedule.rejected, (state, action) => {
            state.loading   = false;
            state.error     = action.payload;
        });
    }
})

export const { setLoadingFalse, resetArchiveLoading } = archiveScheduleSlice.actions;
export default archiveScheduleSlice.reducer;