export default {
    namespaced: true,
    state: {
        permissions: null,
        roles: null,
        routesConfig: null,
    },
    //大量共享的方法
    getters: {
        permissions: state => {
            if (!state.permissions) {
                try {
                    state.permissions = [];
                } catch (e) {
                    console.error(e.message);
                }
            }
            return state.permissions;
        },
        roles: state => {
            if (!state.roles) {
                try {
                    state.roles = [];
                } catch (e) {
                    console.error(e.message);
                }
            }
            return state.roles;
        },
        routesConfig: state => {
            if (!state.routesConfig) {
                try {
                    const routesConfig = localStorage.getItem(process.env.VUE_APP_ROUTES_KEY);
                    state.routesConfig = JSON.parse(routesConfig);
                    state.routesConfig = state.routesConfig ? state.routesConfig : [];
                } catch (e) {
                    console.error(e.message);
                }
            }
            return state.routesConfig;
        },
    },
    mutations: {
        setPermissions(state, permissions) {
            state.permissions = permissions;
            localStorage.setItem(process.env.VUE_APP_PERMISSIONS_KEY, JSON.stringify(permissions));
        },
        setRoles(state, roles) {
            state.roles = roles;
            localStorage.setItem(process.env.VUE_APP_ROLES_KEY, JSON.stringify(roles));
        },
        setRoutesConfig(state, routesConfig) {
            state.routesConfig = routesConfig;
            localStorage.setItem(process.env.VUE_APP_ROUTES_KEY, JSON.stringify(routesConfig));
        },
    },
};
