var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import { decodeJWTPayload, isExpiredJWT } from '../utils';
var User = /** @class */ (function () {
    function User(Api, info, idToken, refreshToken) {
        this.Api = Api;
        this.idToken = idToken;
        this.refreshToken = refreshToken;
        this.setInfo(info);
    }
    User.prototype.setInfo = function (info) {
        var uid = info.uid, providerId = info.providerId, email = info.email, emailVerified = info.emailVerified, createdAt = info.createdAt, lastLogin = info.lastLogin, username = info.username, phoneNumber = info.phoneNumber, displayName = info.displayName, photoURL = info.photoURL, bio = info.bio, url = info.url, address = info.address, additionalData = info.additionalData, claims = info.claims, settings = info.settings, isAnonymous = info.isAnonymous, isNewUser = info.isNewUser;
        this.uid = uid;
        this.providerId = providerId;
        this.email = email;
        this.emailVerified = emailVerified;
        this.createdAt = createdAt;
        this.lastLogin = lastLogin;
        this.username = username;
        this.displayName = displayName;
        this.phoneNumber = phoneNumber;
        this.photoURL = photoURL;
        this.bio = bio;
        this.url = url;
        this.address = address;
        this.additionalData = additionalData;
        this.claims = claims;
        this.settings = settings;
        this.isAnonymous = isAnonymous;
        this.isNewUser = isNewUser;
        return info;
    };
    User.prototype.toJSON = function () {
        var uid = this.uid;
        var providerId = this.providerId;
        var email = this.email;
        var emailVerified = this.emailVerified;
        var createdAt = this.createdAt;
        var lastLogin = this.lastLogin;
        var username = this.username;
        var phoneNumber = this.phoneNumber;
        var displayName = this.displayName;
        var photoURL = this.photoURL;
        var bio = this.bio;
        var url = this.url;
        var address = this.address;
        var additionalData = this.additionalData;
        var claims = this.claims;
        var settings = this.settings;
        var isAnonymous = this.isAnonymous;
        var isNewUser = this.isNewUser;
        return {
            uid: uid,
            providerId: providerId,
            email: email,
            emailVerified: emailVerified,
            createdAt: createdAt,
            lastLogin: lastLogin,
            username: username,
            phoneNumber: phoneNumber,
            displayName: displayName,
            photoURL: photoURL,
            bio: bio,
            url: url,
            address: address,
            additionalData: additionalData,
            claims: claims,
            settings: settings,
            isAnonymous: isAnonymous,
            isNewUser: isNewUser,
        };
    };
    User.prototype.getIdToken = function (forceRefresh) {
        if (forceRefresh === void 0) { forceRefresh = false; }
        return __awaiter(this, void 0, void 0, function () {
            var idToken;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(isExpiredJWT(this.idToken) || forceRefresh)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.Api.get('/token', {
                                refreshToken: this.refreshToken,
                            })];
                    case 1:
                        idToken = (_a.sent()).idToken;
                        this.idToken = idToken;
                        _a.label = 2;
                    case 2: return [2 /*return*/, this.idToken];
                }
            });
        });
    };
    User.prototype.getIdTokenResult = function (forceRefresh) {
        if (forceRefresh === void 0) { forceRefresh = false; }
        return __awaiter(this, void 0, void 0, function () {
            var idToken;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getIdToken(forceRefresh)];
                    case 1:
                        idToken = _a.sent();
                        return [2 /*return*/, decodeJWTPayload(idToken)];
                }
            });
        });
    };
    User.prototype.sendEmailVerification = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!!this.emailVerified) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.Api.put('/oob', {}, {
                                mode: 'verifyEmail',
                                email: this.email,
                            })];
                    case 1: return [2 /*return*/, _a.sent()];
                    case 2: return [2 /*return*/];
                }
            });
        });
    };
    User.prototype.updateProfile = function (profile) {
        return __awaiter(this, void 0, void 0, function () {
            var newInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user', {}, {
                            profile: profile,
                        })];
                    case 1:
                        newInfo = _a.sent();
                        return [2 /*return*/, this.setInfo(newInfo)];
                }
            });
        });
    };
    User.prototype.setAdditionalData = function (data) {
        return __awaiter(this, void 0, void 0, function () {
            var newInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user/additional', {}, {
                            additionalData: data,
                        })];
                    case 1:
                        newInfo = _a.sent();
                        return [2 /*return*/, this.setInfo(newInfo)];
                }
            });
        });
    };
    User.prototype.setSettings = function (data) {
        return __awaiter(this, void 0, void 0, function () {
            var newInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user/settings', {}, {
                            settings: data,
                        })];
                    case 1:
                        newInfo = _a.sent();
                        return [2 /*return*/, this.setInfo(newInfo)];
                }
            });
        });
    };
    User.prototype.setProfilePublicly = function (props) {
        return __awaiter(this, void 0, void 0, function () {
            var newInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user/publicly', {}, {
                            publicly: props,
                        })];
                    case 1:
                        newInfo = _a.sent();
                        return [2 /*return*/, this.setInfo(newInfo)];
                }
            });
        });
    };
    User.prototype.setProfilePrivately = function (props) {
        return __awaiter(this, void 0, void 0, function () {
            var newInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user/privately', {}, {
                            privately: props,
                        })];
                    case 1:
                        newInfo = _a.sent();
                        return [2 /*return*/, this.setInfo(newInfo)];
                }
            });
        });
    };
    User.prototype.setUsername = function (username) {
        return __awaiter(this, void 0, void 0, function () {
            var newInfo;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user/username', {}, {
                            username: username,
                        })];
                    case 1:
                        newInfo = _a.sent();
                        return [2 /*return*/, this.setInfo(newInfo)];
                }
            });
        });
    };
    User.prototype.changePassword = function (currentPassword, newPassword) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.post('/user/password', {}, {
                            currentPassword: currentPassword,
                            newPassword: newPassword,
                        })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    User.prototype.logout = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.delete('/')];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    User.prototype.delete = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.Api.delete('/cancel', {}, {
                            refreshToken: this.refreshToken,
                        })];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    return User;
}());
export { User };
