import Api from '@/services/Api'

export default {
    login(username, password, recaptchaToken) {
        return Api().post('/v1/user/login', {username: username, password: password, "g-recaptcha-response": recaptchaToken});
    },
    register(username, password, mail, invite, recaptchaToken) {
        return Api().post('/v1/user/register', {username: username, password: password, mail: mail, invite: invite, "g-recaptcha-response": recaptchaToken});
    },
    setAccessToken(access_token) {
        localStorage.setItem('access_token', access_token)
        Api().defaults.headers.common['Authorization'] = `Bearer ${access_token}`
    },
    getTokenFromStorage() {
        this.setAccessToken(localStorage.getItem("access_token"));
    },
    me() {
        return Api().get('/v1/user/me')
    },
    mails() {
        return Api().get('/v1/user/me/mails')
    },
    logout() {
        localStorage.removeItem("access_token");
    },
    getMails() {
        return Api().get('/v1/user/me/mails');
    },
    addMail(mail) {
      return Api().post('/v1/user/me/mails', {mail: mail});
    },
    removeMail(mailID) {
        return Api().delete('/v1/user/me/mail/'+mailID);
    },
    markMailAsPrimary(mailID) {
        return Api().post('/v1/user/me/mail/'+mailID+'/markAsPrimary');
    },
    getCaptchaSettings() {
        return Api().get("/v1/user/captcha");
    },
    getInviteCodeInfo(code) {
        return Api().get("/v1/user/invites?code="+code)
    },
    changePassword(newPassword) {
        return Api().post("/v1/user/me/changePassword", {"password": newPassword});
    }
}
