"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deactivate = exports.activate = exports.DEFAULT_SCOPES = void 0;
const vscode = require("vscode");
const AADHelper_1 = require("./AADHelper");
const vscode_extension_telemetry_1 = require("vscode-extension-telemetry");
exports.DEFAULT_SCOPES = 'https://management.core.windows.net/.default offline_access';
async function activate(context) {
    const { name, version, aiKey } = context.extension.packageJSON;
    const telemetryReporter = new vscode_extension_telemetry_1.default(name, version, aiKey);
    const loginService = new AADHelper_1.AzureActiveDirectoryService(context);
    context.subscriptions.push(loginService);
    await loginService.initialize();
    context.subscriptions.push(vscode.authentication.registerAuthenticationProvider('microsoft', 'Microsoft', {
        onDidChangeSessions: AADHelper_1.onDidChangeSessions.event,
        getSessions: (scopes) => loginService.getSessions(scopes),
        createSession: async (scopes) => {
            try {
                /* __GDPR__
                    "login" : {
                        "scopes": { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" }
                    }
                */
                telemetryReporter.sendTelemetryEvent('login', {
                    // Get rid of guids from telemetry.
                    scopes: JSON.stringify(scopes.map(s => s.replace(/[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/i, '{guid}'))),
                });
                const session = await loginService.createSession(scopes.sort().join(' '));
                AADHelper_1.onDidChangeSessions.fire({ added: [session], removed: [], changed: [] });
                return session;
            }
            catch (e) {
                /* __GDPR__
                    "loginFailed" : { }
                */
                telemetryReporter.sendTelemetryEvent('loginFailed');
                throw e;
            }
        },
        removeSession: async (id) => {
            try {
                /* __GDPR__
                    "logout" : { }
                */
                telemetryReporter.sendTelemetryEvent('logout');
                const session = await loginService.removeSession(id);
                if (session) {
                    AADHelper_1.onDidChangeSessions.fire({ added: [], removed: [session], changed: [] });
                }
            }
            catch (e) {
                /* __GDPR__
                    "logoutFailed" : { }
                */
                telemetryReporter.sendTelemetryEvent('logoutFailed');
            }
        }
    }, { supportsMultipleAccounts: true }));
    return;
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() { }
exports.deactivate = deactivate;
//# sourceMappingURL=extension.js.map