"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdalAuthContextManager = void 0;
var tslib_1 = require("tslib");
var AuthenticationContext = tslib_1.__importStar(require("adal-angular"));
var AdalAuthContext_1 = tslib_1.__importDefault(require("./AdalAuthContext"));
var sp_core_library_1 = require("@microsoft/sp-core-library");
var OboTokenProvider_1 = require("./OboTokenProvider");
/**
 * Class for managing multiple instances of the authentication context.
 * @internal
 */
var AdalAuthContextManager = /** @class */ (function () {
    function AdalAuthContextManager() {
        /**
         * Handles the loading of Adal.js. It is set to protected so that it can
         * be mocked in a unit test.
         * @internal
         */
        this._loadAdalJsModulePromise = undefined;
        this._authContextDictionary = new Map();
    }
    AdalAuthContextManager.convertTokenParametersToConfig = function (tokenProviderParameters) {
        var extraQueryParameter;
        var loginHint = tokenProviderParameters.userPrincipalName;
        if (loginHint) {
            extraQueryParameter = "login_hint=" + encodeURIComponent(loginHint);
        }
        var instance = sp_core_library_1.UrlUtilities.removeEndSlash(tokenProviderParameters.aadInstanceUrl) + '/';
        return {
            clientId: tokenProviderParameters.servicePrincipalId,
            redirectUri: tokenProviderParameters.redirectUri,
            instance: instance,
            loadFrameTimeout: 10000,
            tenant: tokenProviderParameters.aadTenantId,
            navigateToLoginRequestUrl: false,
            extraQueryParameter: extraQueryParameter
        };
    };
    AdalAuthContextManager.prototype.getOboTokenProvider = function (tokenProviderConfiguration, sharePointOBOProviderConfiguration, oboFirstyPartyTokenCallback) {
        return new OboTokenProvider_1.OboTokenProvider(AuthenticationContext.inject(AdalAuthContextManager.convertTokenParametersToConfig(tokenProviderConfiguration)), sharePointOBOProviderConfiguration, oboFirstyPartyTokenCallback);
    };
    // Adal-angular exports include an inject function that takes a AuthenticationContext
    // configuration object and returns a instance of the AuthenticationContext.
    // https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/dev/lib/adal.js
    AdalAuthContextManager.prototype.getAdalTokenProvider = function (tokenProviderConfiguration) {
        // Using the servicePrincipalId as a unique identiifier for Authentication Context objects
        if (!this._authContextDictionary.has(tokenProviderConfiguration.servicePrincipalId)) {
            var authContext = new AdalAuthContext_1.default(AuthenticationContext.inject(AdalAuthContextManager.convertTokenParametersToConfig(tokenProviderConfiguration)), tokenProviderConfiguration.aadUserId);
            this._authContextDictionary.set(tokenProviderConfiguration.servicePrincipalId, authContext);
        }
        return this._authContextDictionary.get(tokenProviderConfiguration.servicePrincipalId);
    };
    return AdalAuthContextManager;
}());
exports.AdalAuthContextManager = AdalAuthContextManager;
//# sourceMappingURL=AdalAuthContextManager.js.map