"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.user = void 0;
const utils_1 = require("../../utils");
const store_1 = require("../../store");
function configDomain(code) {
    const domain = utils_1.getDomainByCountryCode(code);
    if (domain === '') {
        return false;
    }
    else {
        store_1.setDomain(domain);
        return true;
    }
}
exports.user = {
    login(params) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!configDomain(params.countryCode)) {
                return {
                    error: 91001,
                    msg: '无效的国家码'
                };
            }
            const { error, msg, data } = yield utils_1.sendRequest('/v2/user/login', 'POST', params);
            if (error === 0) {
                const { at, rt } = data;
                store_1.setAt(at);
                store_1.setRt(rt);
            }
            return {
                error,
                msg,
                data
            };
        });
    },
    logout() {
        return __awaiter(this, void 0, void 0, function* () {
            const ret = yield utils_1.sendRequest('/v2/user/logout', 'DELETE', null, store_1.getAt());
            if (ret.error === 0) {
                store_1.setAt('');
                store_1.setRt('');
            }
            return ret;
        });
    },
    changePwd(params) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield utils_1.sendRequest('/v2/user/change-pwd', 'POST', params, store_1.getAt());
        });
    },
    getProfile() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield utils_1.sendRequest('/v2/user/profile', 'GET', null, store_1.getAt());
        });
    },
    updateProfile(params) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield utils_1.sendRequest('/v2/user/profile', 'POST', params, store_1.getAt());
        });
    },
    refresh() {
        return __awaiter(this, void 0, void 0, function* () {
            const { error, msg, data } = yield utils_1.sendRequest('/v2/user/refresh', 'POST', { rt: store_1.getRt() }, store_1.getAt());
            if (error === 0) {
                const { at, rt } = data;
                store_1.setAt(at);
                store_1.setRt(rt);
            }
            return {
                error,
                msg,
                data
            };
        });
    },
    register(params) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!configDomain(params.countryCode)) {
                return {
                    error: 91001,
                    msg: '无效的国家码'
                };
            }
            return yield utils_1.sendRequest('/v2/user/register', 'POST', params);
        });
    },
    sendVerificationCode(params) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield utils_1.sendRequest('/v2/user/verification-code', 'POST', params);
        });
    },
    smsLogin(params) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!configDomain(params.countryCode)) {
                return {
                    error: 91001,
                    msg: '无效的国家码'
                };
            }
            return yield utils_1.sendRequest('/v2/user/sms-login', 'POST', params);
        });
    },
    resetPwd(params) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield utils_1.sendRequest('/v2/user/reset-pwd', 'POST', params);
        });
    },
    closeAccount(params) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield utils_1.sendRequest('/v2/user/close-account', 'POST', params);
        });
    }
};
