import jwt_decode from "jwt-decode";
export default {
    state: {
        resetPasswordEmail: '',
        gRecaptchaResponse: null,
        updatedFullname:localStorage.getItem('updated-fullname') || '',
        authentication: {
            access_token: localStorage.getItem('user-token') || ''
        }
    },
    getters: {
        isAuthenticated: 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,
        isCustomer: (state, getters) => getters.getDecodedAccessToken.user.type === 3,
        isDestinationAccountAdmin: (state, getters) => getters.getDecodedAccessToken.user.type === 7,
        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,
        getAccessToken(state, getters, dispatch) {

            // if(getters.isAuthenticated){
            //     $crisp.push(["set", "user:email", getters.getUserEmail]);
            //     if(getters.isAdmin){
            //         $crisp.push(["set", "user:nickname", getters.getUserName]);
            //     }

            //     if(getters.isCustomer && ('company_marking' in getters.getDecodedAccessToken.user)){
            //         $crisp.push(["set", "user:nickname", getters.getDecodedAccessToken.user.company_marking]);
            //         $crisp.push(["set", "session:data", [
            //             [
            //                 ["exchange-marking", getters.getDecodedAccessToken.user.company_marking],
            //                 ["company-name", getters.getDecodedAccessToken.user.company_name]
            //             ]
            //         ]]);
            //     }
            // }

            return state.authentication.access_token
        },
        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,
        getReCaptcha: state => state.gRecaptchaResponse

    },
    mutations: {
        SET_AUTHENTICATION(state, {access_token}){
            state.authentication.access_token = access_token;
        },
        SET_GOOGLE_RE_CAPTCHA(state, {token}){
            state.gRecaptchaResponse = 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);

            localStorage.removeItem('updated-fullname');

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

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

            setTimeout(function(){
                window.location.href = redirect_url;
            }, 2000);
        },
        reCaptcha(store, {token}){
            store.commit('SET_GOOGLE_RE_CAPTCHA', {token});
        },
        async ensureReCaptchaIsSet(store) {
            return new Promise(wait);

            function wait(resolve){
                if (store.state.gRecaptchaResponse) return resolve(store.state.gRecaptchaResponse);
                else setTimeout(wait.bind(this, resolve), 30);
            }
        },
        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});
        }
    }
}