import config from '@/config'
import {storage} from '@/utils/storage'

const {fixedHeader, sidebarLogo, filterShrink, paramCache, closeConfirmEnable} = config;

export default {
    namespaced: true,
    state: {
        sidebar: {
            opened: storage.getData('sidebarStatus') ? !!+storage.getData('sidebarStatus') : true,
            withoutAnimation: false
        },
        device: 'desktop',
        fixedHeader: fixedHeader,
        sidebarLogo: sidebarLogo,
        filterShrink: filterShrink,
        paramCache: paramCache,
        closeConfirmEnable: closeConfirmEnable,
    },
    getters: {
        sidebar: state => state.sidebar,
        device: state => state.device,
    },
    mutations: {
        TOGGLE_SIDEBAR: state => {
            state.sidebar.opened = !state.sidebar.opened;
            state.sidebar.withoutAnimation = false;
            if (state.sidebar.opened) {
                storage.setData('sidebarStatus', 1)
            } else {
                storage.setData('sidebarStatus', 0)
            }
        },
        CLOSE_SIDEBAR: (state, withoutAnimation) => {
            if (state.sidebar.opened) {
                storage.setData('sidebarStatus', 0);
                state.sidebar.opened = false;
            }
            state.sidebar.withoutAnimation = withoutAnimation
        },
        TOGGLE_DEVICE: (state, device) => {
            state.device = device
        },
        CHANGE_SETTING: (state, {key, value}) => {
            if (state.hasOwnProperty(key)) {
                state[key] = value
            }
        }
    },
    actions: {
        toggleSideBar({commit}) {
            commit('TOGGLE_SIDEBAR')
        },
        closeSideBar({commit}, {withoutAnimation}) {
            commit('CLOSE_SIDEBAR', withoutAnimation)
        },
        toggleDevice({commit}, device) {
            commit('TOGGLE_DEVICE', device)
        },
        changeSetting({commit}, data) {
            commit('CHANGE_SETTING', data)
        }
    }
}
