"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryWecomLogin = void 0;
const tslib_1 = require("tslib");
const helper_1 = require("./helper");
const router_1 = require("./router");
const types_1 = require("./types");
const index_1 = require("./index");
const _CACHE_MAP = {};
function tryWecomLogin(location, fallback) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            if (!_CACHE_MAP[location.href]) {
                _CACHE_MAP[location.href] = wecomLogin(location);
            }
            const res = yield _CACHE_MAP[location.href];
            if (res) {
                _CACHE_MAP[location.href] = undefined;
                return;
            }
            else {
                fallback();
            }
        }
        catch (e) {
            console.error('企微原地登录失败，fallback to sso登录', e);
            fallback();
        }
    });
}
exports.tryWecomLogin = tryWecomLogin;
function wecomLogin(location) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const router = new router_1.Router(document.createElement('div'), location);
        const config = yield (0, helper_1.getConfig)(router);
        const loginMethods = (config === null || config === void 0 ? void 0 : config.web) || [];
        const loginTypes = [];
        const authList = [];
        let wecomConfig = undefined;
        loginMethods.forEach((item) => {
            if (!loginTypes.includes(item.type)) {
                loginTypes.push(item.type);
            }
            if (item.type === types_1.LoginMethodType.AUTH) {
                authList.push(item);
            }
            if (item.id === 'wecom') {
                wecomConfig = item;
            }
        });
        // if (loginTypes.length === 1 && authList.length === 1 && !config?.agreement?.enable) {
        if (wecomConfig) {
            yield (0, index_1.providerLogin)(router, wecomConfig.id);
            return true;
        }
        else {
            return false;
        }
    });
}
//# sourceMappingURL=init.js.map