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

interface CommonState {
    resources: [] | null;
    meal_tags: [] | null;
    configs: any | null;
    duration: {
        min: number;
        max: number;
        step: number;
    } | null;
    showTabbar: boolean | true;
    pageIndex: number | 0;
}

const initialState: CommonState = {
    resources: [],
    meal_tags: [],
    configs: null,
    duration: null,
    showTabbar: true,
    pageIndex: 0
}

const commonSlice = createSlice({
    name: 'permission',
    initialState,
    reducers: {
        //静态资源
        setResources(state, action) {
            state.resources = action.payload;
        },
        setMealTags(state, action) {
            state.meal_tags = action.payload
        },
        //是否disable并隐藏tabbar
        setTabbarStatus(state, action) {
            state.showTabbar = action.payload;
        },
        //time配置文件
        setConfigs(state, action) {
            state.configs = action.payload;

            if (state.configs) {

                state.duration = {
                    min: state.configs.min / 60,
                    max: state.configs.max / 60,
                    step: state.configs.step
                }

            }
        },
        changeTabbar(state, action) {
            state.pageIndex = action.payload;
        }
    }
});

export default commonSlice.reducer;
export const { setResources, setMealTags, setConfigs, setTabbarStatus, changeTabbar } = commonSlice.actions;