import * as SecureStore from 'expo-secure-store';
import {LOGGING_IN} from "./mutations";

export async function LOGIN({ commit, state }, { userObj, navigate }) {
    commit('LOGGING_IN', true);

    userObj = JSON.stringify(userObj);

    return new Promise((resolve, reject) => {
        commit('LOGIN_SUCCESSFUL', { userObj });
        setTimeout(() => {
            SecureStore.setItemAsync('user', userObj).then(() => {
                navigate('Drawer');
                resolve();
            });
        }, 1000)
    });
}

export function SET_USER({ commit, state }, { userObj }) {
    userObj = JSON.stringify(userObj)
    return commit('LOGIN_SUCCESSFUL', { userObj });
}

export function LOGOUT({ commit, state }, callback) {
    return new Promise((resolve, reject) => {
        SecureStore.deleteItemAsync('user').then(() => {
            callback();
            resolve();
        })
    })
}
