import {createSlice} from '@reduxjs/toolkit'
import {cacheInfo} from "../../Utils/cachedInfoUtils.js";

const initialState = {
    searchText: '',
    sortOrder: 'asc',
    viewMode: 'tile',
    currentPage: 1,
    pageSize: 10,
    selectedPatient: null,
    isModalVisible: false
};

const patientsSlice = createSlice({
    name: 'patientsUI',
    initialState,
    reducers: {
        setSearchText: (state, action) => {
            state.searchText = action.payload;
        },
        setSortOrder: (state, action) => {
            state.sortOrder = action.payload;
        },
        setViewMode: (state, action) => {
            state.viewMode = action.payload;
            cacheInfo("viewModePatients", action.payload);
        },
        setCurrentPage: (state, action) => {
            state.currentPage = action.payload;
        },
        setPageSize: (state, action) => {
            state.pageSize = action.payload;
        },
        openModal: (state) => {
            state.isModalVisible = true;
        },
        closeModal: (state) => {
            state.isModalVisible = false;
            state.selectedPatient = null;
        },
        selectPatient: (state, action) => {
            state.selectedPatient = action.payload;
        }
    }
})

export const {
    setSearchText,
    setSortOrder,
    setViewMode,
    setCurrentPage,
    setPageSize,
    openModal,
    closeModal,
    selectPatient
} = patientsSlice.actions;

export default patientsSlice.reducer;