/**
 * File: commonSlice.ts
 * Description:
 * - This file defines the Redux slice for managing common 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 common scenarios such as isLoggedIn, etc.
*/


import { getEncryptedCookie } from '@/src/utils/commonFunctions';
import { AUTH_COOKIES } from '@/src/utils/commonVariables';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface CommonState {
    isLoggedIn: boolean;
    LRFFlow: null;
    LRFModalOpen: boolean;
    BookingTimer: null;
    PropertyView: string;
    LRFData: any;
    showDraftedModal: boolean;
    afterDraftedNavigation: string;
    isBookingFlow: boolean;
}

const authToken = getEncryptedCookie(AUTH_COOKIES.USER_TOKEN)

const initialState: CommonState = {
    isLoggedIn: authToken ? true : false,
    LRFFlow: null,
    LRFModalOpen: false,
    BookingTimer: null,
    PropertyView: 'list',
    LRFData: null,
    showDraftedModal: false,
    afterDraftedNavigation: '',
    isBookingFlow: false
}

const common = createSlice({
    name: 'common',
    initialState,
    reducers: {
        /**
         * Used to set the isLoggedIn state
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which which passed
         */
        setIsLoggedIn: (state: CommonState, action: PayloadAction<boolean>) => {
            return { ...state, isLoggedIn: action.payload };
        },

        /**
         * Used to set the LRFFlow state
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which passed
         */
        setLRFFlow: (state: CommonState, action: PayloadAction<any>) => {
            return { ...state, LRFFlow: action.payload };
        },

        /**
         * Used to set the LRFModalOpen state
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which passed
         */
        setLRFModalOpen: (state: CommonState, action: PayloadAction<any>) => {
            return { ...state, LRFModalOpen: action.payload };
        },

        /**
         * Used to set the LRFFlow state
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which passed
         */
        setLRFData: (state: CommonState, action: PayloadAction<any>) => {
            return { ...state, LRFData: action.payload };
        },

        /**
         * Used to set the LRFModalOpen state
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which passed
         */
        setBookingTimer: (state: CommonState, action: PayloadAction<any>) => {
            return { ...state, BookingTimer: action.payload };
        },

        /**
         * Used to set the PropertyView state
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which passed
         */
        setPropertyView: (state: CommonState, action: PayloadAction<any>) => {
            return { ...state, PropertyView: action.payload };
        },

        /**
         * Used to get the actual success response of draftedModal
         * @param state: State is the object data of redux store
         * @param action: action is used to store the data which is passed by user
         */
        draftedModal: (state: CommonState, action: PayloadAction<any>) => {
            return {
                ...state,
                showDraftedModal: action.payload,
            };
        },
        /**
         * Used to get the actual success response of afterDraftedNavigation
         * @param state: State is the object data of redux store
         * @param action: action is used to store the data which is passed by user
         */
        afterDraftedNavigation: (state: CommonState, action: PayloadAction<any>) => {
            return {
                ...state,
                afterDraftedNavigation: action.payload,
            };
        },

        /**
         * Used to get the actual success response of isEditFlow
         * @param state: State is the object data of redux store
         * @param action: action is used to store the data which is passed by user
         */
        isBookingFlow: (state: CommonState, action: PayloadAction<any>) => {
            return {
                ...state,
                isBookingFlow: action.payload,
            };
        },
    },
});

export const commonActions = common.actions
export default common.reducer;