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

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

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

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

export default lensesSlice.reducer;