"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtensionsActivator = exports.ActivatedExtension = exports.ExtensionActivationTimes = void 0;
class ExtensionActivationTimes {
    constructor(startup, codeLoadingTime, activateCallTime, activateResolvedTime) {
        this.startup = startup;
        this.codeLoadingTime = codeLoadingTime;
        this.activateCallTime = activateCallTime;
        this.activateResolvedTime = activateResolvedTime;
    }
}
exports.ExtensionActivationTimes = ExtensionActivationTimes;
ExtensionActivationTimes.NONE = new ExtensionActivationTimes(false, -1, -1, -1);
class ActivatedExtension {
    constructor(id, displayName, description, host, activationFailed, activationFailedError, module, exports, subscriptions, activationTimes, extendExports, extendModule) {
        this.id = id;
        this.displayName = displayName;
        this.description = description;
        this.host = host;
        this.activationFailed = activationFailed;
        this.activationFailedError = activationFailedError;
        this.module = module;
        this.exports = exports;
        this.subscriptions = subscriptions;
        this.activationTimes = activationTimes;
        this.extendExports = extendExports;
        this.extendModule = extendModule;
    }
    toJSON() {
        var _a;
        return {
            id: this.id,
            displayName: this.displayName,
            description: this.description,
            host: this.host,
            activationFailed: this.activationFailed,
            activationFailedError: this.activationFailedError,
            activateCallTime: (_a = this.activationTimes) === null || _a === void 0 ? void 0 : _a.activateCallTime,
        };
    }
}
exports.ActivatedExtension = ActivatedExtension;
class ExtensionsActivator {
    constructor(logger = console) {
        this.logger = logger;
        this.activatedExtensions = new Map();
    }
    has(id) {
        return this.activatedExtensions.has(id);
    }
    set(id, extension) {
        return this.activatedExtensions.set(id, extension);
    }
    get(id) {
        return this.activatedExtensions.get(id);
    }
    all() {
        return Array.from(this.activatedExtensions.values());
    }
    delete(id) {
        return this.activatedExtensions.delete(id);
    }
    async doDeactivate(extensionModule) {
        try {
            const promiseLike = await extensionModule.deactivate();
            return promiseLike;
        }
        catch (err) {
            this.logger.error(`
        [Extension-Host] deactivate extension module error ${err.message} \n\n
        Stack: ${err.stack && err.stack}
      `);
            return err;
        }
    }
    deactivate() {
        const deactivateTasks = [];
        this.activatedExtensions.forEach((ext) => {
            const extModule = ext.module;
            if (extModule && extModule.deactivate && typeof extModule.deactivate === 'function') {
                deactivateTasks.push(this.doDeactivate(extModule));
            }
            const extendModule = ext.extendModule;
            if (extendModule && extendModule.deactivate && typeof extendModule.deactivate === 'function') {
                deactivateTasks.push(this.doDeactivate(extendModule));
            }
            ext.subscriptions.forEach((disposable) => {
                try {
                    disposable.dispose();
                }
                catch (e) {
                    this.logger.log('extension deactivated error');
                    this.logger.warn(e);
                }
            });
        });
        return Promise.all(deactivateTasks);
    }
}
exports.ExtensionsActivator = ExtensionsActivator;
//# sourceMappingURL=activator.js.map