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

const initialState = {
    searchText: '',
    currentPage: 1,
    pageSize: 10,
    selectedIssue: null,
    isModalVisible: false,
    viewMode: 'table',
    startFilterDate: null,
    endFilterDate: null,
};

const lensIssuesSlice = createSlice({
    name: 'setIssuesUI',
    initialState,
    reducers: {
        setSearchText: (state, action) => {
            state.searchText = action.payload;
        },
        setCurrentPage: (state, action) => {
            state.currentPage = action.payload;
        },
        setPageSize: (state, action) => {
            state.pageSize = action.payload;
        },
        selectIssue: (state, action) => {
            state.selectedIssue = action.payload;
        },
        openModal: (state) => {
            state.isModalVisible = true;
        },
        closeModal: (state) => {
            state.isModalVisible = false;
            state.selectedIssue = null;
        },
        setViewMode: (state, action) => {
            state.viewMode = action.payload;
            cacheInfo("viewModeIssues", action.payload);
        },
        setStartFilterDate: (state, action) => {
            state.startFilterDate = action.payload;
        },
        setEndFilterDate: (state, action) => {
            state.endFilterDate = action.payload;
        },
    },
});

export const {
    setSearchText,
    setCurrentPage,
    setPageSize,
    selectIssue,
    openModal,
    closeModal,
    setViewMode,
    setStartFilterDate,
    setEndFilterDate,
} = lensIssuesSlice.actions;

export default lensIssuesSlice.reducer;