import jwt_decode from "jwt-decode";
export default {
    state: {
        resetPasswordEmail: '',
        updatedFullname:localStorage.getItem('updated-fullname') || '',
        authentication: {
            access_token: localStorage.getItem('user-token') || ''
        }
    },
    getters: {
        isAuthenticated: state => !!state.authentication.access_token,
        getAccessToken: state => state.authentication.access_token,
        isSuperAdmin: (state, getters) => getters.getDecodedAccessToken.user.type === 0 || getters.getDecodedAccessToken.user.type === 1,
        isAdmin: (state, getters) => getters.getDecodedAccessToken.user.type === 0 || getters.getDecodedAccessToken.user.type === 1 || getters.getDecodedAccessToken.user.type === 2,
        getUserName: (state, getters) => getters.getUpdatedFullname || getters.getDecodedAccessToken.user.name,
        getUserId: (state, getters) => getters.getDecodedAccessToken.user.id,
        getUserEmail: (state, getters) => getters.getDecodedAccessToken.user.email,
        getCompanyId: (state, getters) => getters.getDecodedAccessToken.user.company_id,
        getCompanyModuleId: (state, getters) => getters.getDecodedAccessToken.user.company_module_id,
        getOrder: (state, getters) => getters.getDecodedAccessToken.user.order,

        getDecodedAccessToken(state) {
            try{
                return jwt_decode(state.authentication.access_token);
            }
            catch(Error){
                return {
                    user: {
                        id: '',
                        name: '',
                        role: '',
                        status: 0,
                    }

                };
            }
        },

        getResetPasswordEmail: state => state.resetPasswordEmail,
        getUpdatedFullname: state => state.updatedFullname
    },
    mutations: {
        SET_AUTHENTICATION(state, {access_token}){
            state.authentication.access_token = access_token;
        },
        SET_PASSWORD_RESET_EMAIL(state, {email}){
            state.resetPasswordEmail = email;
        },
        UPDATE_FULL_NAME(state, {fullname}){
            state.updatedFullname = fullname;
        }
    },
    actions: {
        userAuthentication(store, {access_token, redirect_url}){
            localStorage.setItem('user-token', access_token);

            store.commit('SET_AUTHENTICATION', {access_token});

            store.dispatch('toggleLoading', {name: 'loginSection', status: true});

            setTimeout(function(){
                window.location.href = redirect_url;
            }, 2000);
        },
        updateToken(store, {access_token}){

            localStorage.setItem('user-token', access_token);

            store.commit('SET_AUTHENTICATION', {access_token});

        },
        passwordResetEmail(store, {email}){
            store.commit('SET_PASSWORD_RESET_EMAIL', {email});
            store.dispatch('toggleLoading', {name: 'forgetPasswordSection', status: true});
            setTimeout(function(){
                store.dispatch('toggleLoading', {name: 'forgetPasswordSection', status: false});
            }, 2000);
        },
        passwordReset(store){
            store.dispatch('toggleLoading', {name: 'resetPasswordSection', status: true});
        },
        returnToLoginAfterSendResetPasswordLink(store){
            store.dispatch('toggleSection', {name: 'forgetPassword', status: false})
            store.dispatch('toggleSection', {name: 'forgetPasswordSuccess', status: false});
        },
        updateProfile(store, {fullname}){
            localStorage.setItem('updated-fullname', fullname);
            store.commit('UPDATE_FULL_NAME', {fullname});
        }
    }
}