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


const initialState = {
    searchText: '',
    viewMode: 'tile',
    currentPage: 1,
    pageSize: 10,
    selectedLens: null,
    isModalVisible: false,
    showAdvancedSearch: false,
    searchParams: {
        tor: null,
        diameter: null,
        preset_refraction: null,
        periphery_toricity: null,
        side: 'all',
        issued: false,
        trial: null
    },
};

const lensesSlice = createSlice({
    name: 'lensesUI',
    initialState,
    reducers: {
        setSearchText: (state, action) => {
            state.searchText = action.payload;
        },
        setCurrentPage: (state, action) => {
            state.currentPage = action.payload;
        },
        setPageSize: (state, action) => {
            state.pageSize = action.payload;
        },
        setViewMode: (state, action) => {
            state.viewMode = action.payload;
            cacheInfo("viewModeLenses", action.payload);
        },
        openModal: (state) => {
            state.isModalVisible = true;
        },
        closeModal: (state) => {
            state.isModalVisible = false;
            state.selectedLens = null;
        },
        selectLens: (state, action) => {
            state.selectedLens = action.payload;
        },
        setSearchParams: (state, action) => {
            state.searchParams = action.payload;
        },
        setShowAdvancedSearch: (state, action) => {
            state.showAdvancedSearch = action.payload;
        },
    }
});

export const {
    setSearchText,
    setCurrentPage,
    setPageSize,
    setViewMode,
    openModal,
    closeModal,
    selectLens,
    setSearchParams,
    setShowAdvancedSearch
} = lensesSlice.actions;

export default lensesSlice.reducer;