"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCommonError = exports.parseValidateError = exports.parseProviderTokenError = exports.parseResetPasswordError = exports.parseProviderSignError = exports.parseVerifyError = exports.parseSignUpError = exports.parseLoginError = exports.errorPage = void 0;
function prefixMsg(msg, prefix = ' - ') {
    return `${msg ? prefix + msg : ''}`;
}
function errorPage(router) {
    return (e, err) => {
        var _a;
        let title;
        let desc;
        switch (err.error) {
            case 'unreachable':
                title = '网络错误';
                desc = `请检查您的网络连接，稍后重试${prefixMsg(e.error_description)}`;
                break;
            case 'permission_denied':
                title = '权限不足';
                desc = `错误详情 - ${err.error_description}`;
                break;
            case '-':
                desc = err.error_description;
                break;
            case 'unauthorized_organization':
                // @todo @nanxi企微场景
                desc = '该应用可能未在您的企业安装，请重新安装，然后5分钟后重试';
                break;
            case '-':
                desc = err.error_description;
                break;
            case 'success':
                (_a = e.querySelector('.icon')) === null || _a === void 0 ? void 0 : _a.classList.add('hidden');
                title = '操作成功';
                desc = err.error_description;
                break;
            default:
                desc = `错误详情 - ${err.error || err.message}${prefixMsg(err.error_description)}`;
                break;
        }
        if (title) {
            e.querySelector('.title').innerText = title;
        }
        if (desc) {
            e.querySelector('.desc').innerText = desc;
        }
        // 异常上报
        if (router.getUrlParams('redirect_uri')) {
            const redirectBtn = e.querySelector('.redirect-btn');
            redirectBtn.style.display = 'block';
            redirectBtn.onclick = (e) => {
                e.preventDefault();
                router.redirectURIorBackTo();
            };
        }
        router.loaded();
    };
}
exports.errorPage = errorPage;
// 登录错误处理
const parseLoginError = (e) => {
    switch (e.error) {
        case 'not_found':
            return '用户不存在';
        case 'password_not_set':
            return '当前用户未设置密码，请使用验证码登录或第三方登录方式';
        case 'invalid_password':
            if (e.details && e.details.length > 0) {
                return `密码验证失败, 您还有 ${e.details[0].remaining} 次重试机会`;
            }
            return '密码不正确';
        case 'user_pending':
            return '该用户未激活';
        case 'user_blocked':
            return '该用户被停用';
        case 'invalid_status':
            if (e.details && e.details.length > 0) {
                const retryInSeconds = e.details[0].retry_in;
                const retryInTime = retryInSeconds < 3600 ? `${retryInSeconds / 60}分钟` : `${retryInSeconds / 3600}小时`;
                return `您已经超过了密码最大重试次数，请于 ${retryInTime} 后重试`;
            }
            return '您已经超过了密码最大重试次数，请稍后重试';
        default:
            return '您输入的账号或密码不正确，请重新输入';
    }
};
exports.parseLoginError = parseLoginError;
// 注册错误处理
const parseSignUpError = (e) => {
    switch (e.error) {
        case 'failed_precondition':
            return '您输入的手机号或邮箱已被注册，请使用其他号码';
        default:
            return (0, exports.parseVerifyError)(e);
    }
};
exports.parseSignUpError = parseSignUpError;
// 验证码验证token
const parseVerifyError = (e) => {
    switch (e.error) {
        case 'invalid_argument':
            return '您输入的验证码不正确或已过期';
        case 'aborted':
            return '您尝试的次数过多，稍后重试';
        default:
            return (0, exports.parseCommonError)(e);
    }
};
exports.parseVerifyError = parseVerifyError;
// 三方登录表单错误
const parseProviderSignError = (e) => {
    switch (e.error) {
        case 'permission_denied':
            return '您当前的会话已过期，请返回重试';
        case 'invalid_argument':
            return '您输入的验证码不正确或已过期';
        case 'aborted':
            return '您尝试的次数过多，稍后重试';
        default:
            return (0, exports.parseCommonError)(e);
    }
};
exports.parseProviderSignError = parseProviderSignError;
// 重置密码错误
const parseResetPasswordError = (e) => {
    switch (e.error) {
        case 'invalid_argument':
            return '您输入的密码不符合要求, 请重新输入';
        default:
            return (0, exports.parseCommonError)(e);
    }
};
exports.parseResetPasswordError = parseResetPasswordError;
// 验证码验证token
const parseProviderTokenError = (e) => {
    switch (e.error) {
        case 'failed_precondition':
            return `从第三方获取用户信息失败${prefixMsg(e.error_description)}`;
        case 'not_found':
            return `没有找到匹配的用户\n请联系管理员确认您的配置`;
        default:
            return (0, exports.parseCommonError)(e);
    }
};
exports.parseProviderTokenError = parseProviderTokenError;
function parseValidateError(e, verificationTarget) {
    switch (e.error) {
        case 'failed_precondition':
            switch (verificationTarget) {
                case 'USER':
                    return '你输入的手机号或邮箱已被注册，请使用其他号码';
                case 'NOT_USER':
                    return '该用户已被注册，请使用其他号码';
                default:
                    return '短信功能未开启，请联系管理员开启';
            }
        case 'payment_required':
            return '短信包剩余条数不足，请联系管理员充值';
        case 'aborted':
            return '你尝试的次数过多，稍后重试';
        case 'unavailable':
            return `发件服务不可用，请稍后重试${prefixMsg(e.error_description)}`;
        default:
            return (0, exports.parseCommonError)(e);
    }
}
exports.parseValidateError = parseValidateError;
const parseCommonError = (e) => {
    let msg;
    switch (e.error) {
        case 'unreachable':
            msg = `网络错误，请检查您的网络连接，稍后重试${prefixMsg(e.error_description)}`;
            break;
        case 'resource_exhausted':
            return '您尝试过于频繁，请稍后重试';
        default:
            msg = `服务异常, 请稍后重试${prefixMsg(e.error || e.message)}${prefixMsg(e.error_description)}`;
            break;
    }
    return msg;
};
exports.parseCommonError = parseCommonError;
//# sourceMappingURL=error-handler.js.map