"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.signInWidthProviderCode = exports.providerLogin = void 0;
const tslib_1 = require("tslib");
const global_api_1 = require("../../app/global-api");
// import { saveUrlParams } from './cookies';
// 用于三方登录跳转回的页面地址生成：默认为当前页面
function getProviderRedirectURI(providerId) {
    return location.href;
    // return location.origin + location.pathname;
}
function providerLogin(router, providerId, scope) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            // if (providerId === PORTAL_WECOM_METHOD.id) {
            //   debugger;
            //   try {
            //     await checkWeChatPermission(router.getUrlParams('env_id'));
            //     await portalWecomLogin();
            //   } catch (e) {
            //     app.showToast({
            //       title: e.message,
            //       icon: 'error',
            //     });
            //   }
            //   return;
            // }
            const cloudbase = yield global_api_1.app.cloud.getCloudInstance();
            const auth = cloudbase.authInstance || cloudbase.auth;
            const state = `prd-${providerId}-${Math.random().toString(36).slice(2)}`;
            // 保存当前页面参数，用于授权回跳使用
            // saveUrlParams(state, router);
            const res = yield auth.genProviderRedirectUri({
                provider_id: providerId,
                provider_redirect_uri: getProviderRedirectURI(providerId),
                state,
                scope,
            });
            location.replace(res.uri);
        }
        catch (err) {
            global_api_1.app.showToast({
                title: err.message,
                icon: 'error',
            });
        }
    });
}
exports.providerLogin = providerLogin;
function signInWidthProviderCode(state, code) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const providerId = state.replace(/prd-(.+)-.+/, '$1');
        const cloudbase = yield global_api_1.app.cloud.getCloudInstance();
        const auth = cloudbase.authInstance || cloudbase.auth;
        const res = yield auth.grantProviderToken({
            provider_id: providerId,
            provider_code: code,
            // provider_redirect_uri: getProviderRedirectURI(providerId),
        });
        return yield auth.signInWithProvider({
            provider_id: providerId,
            provider_token: res.provider_token,
        });
    });
}
exports.signInWidthProviderCode = signInWidthProviderCode;
//# sourceMappingURL=index.js.map