/**
 * File: masterSlice.ts
 * Description:
 * - This file defines the Redux slice for managing master-related state in the application.
 * - It uses Redux Toolkit's `createSlice` to simplify state management and reduce boilerplate code.
 * - The slice contains actions and reducers for handling various master scenarios such as master, etc.
*/


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

interface MasterState {
    isLoading: boolean;
    countryList: any;
    mediaUpload: any;
    mediaUploadLoading: boolean;
}

const initialState: MasterState = {
    isLoading: false,
    countryList: null,
    mediaUpload: null,
    mediaUploadLoading: false,
}


const masterSlice = createSlice({
    name: 'master',
    initialState,
    reducers: {
        /**
         * Used to start the request of master api call
         * @param state: State is initial state of master
         */
        RequestMasterCountryList: (state) => {
            state.isLoading = true;
        },
        /**
         * Used to set the master data
         * @param state: State is initial state of master
         * @param action: Action is data of master success response
         */
        SuccessMasterCountryList: (state, action: PayloadAction<any>) => {
            state.countryList = action.payload;
        },
        /**
         * Used to set the error of master api call
         * @param state: State is initial state of master
         */
        ErrorMasterCountryList: (state) => {
            state.countryList = null;
        },

                /**
         * Used to start the request of media upload api call
         * @param state: State is the object data of redux store
         */
        requestMediaUpload: (state: MasterState) => {
            return { ...state, mediaUploadLoading: true };
        },

        /** Used to get the actual success response of the media upload api
         * @param state: State is the object data of redux store
         * @param action: action is used to store the data which is set on success of media upload api
         */
        responseMediaUpload: (state: MasterState, action: PayloadAction<any>) => {
            return { ...state, mediaUploadLoading: false, mediaUpload: action.payload };
        },

        /**
         * Used to handle the error of the media upload api
         * @param state: State is the object data of redux store
         */
        errorMediaUpload: (state: MasterState) => {
            return { ...state, mediaUploadLoading: false };
        },

        /**
         * Used to start the request of media delete api call
         * @param state: State is the object data of redux store
         */
        requestMediaDelete: (state: MasterState) => {
            return { ...state, masterLoading: true };
        },

        /**
         * Used to get the actual success response of the media delete api
         * @param state: State is the object data of redux store
         */
        responseMediaDelete: (state: MasterState, action: PayloadAction<any>) => {
            return { ...state, masterLoading: false, mediaUpload: action.payload };
        },

        /**
         * Used to handle the error of the media delete api
         * @param state: State is the object data of redux store
         */
        errorMediaDelete: (state: MasterState) => {
            return { ...state, masterLoading: false };
        },
    },
});

export const masterActions = masterSlice.actions
export default masterSlice.reducer;
