
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { IExtensionsWorkbenchService } from '../common/extensions.service.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.service.js';
import { CommandsRegistry } from '../../../../platform/commands/common/commands.js';
import { MenuRegistry, MenuId } from '../../../../platform/actions/common/actions.js';
import { localize } from '../../../../nls.js';
import { areSameExtensions } from '../../../../platform/extensionManagement/common/extensionManagementUtil.js';
import '../../../../platform/notification/common/notification.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { Action } from '../../../../base/common/actions.js';
import { IHostService } from '../../../services/host/browser/host.service.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { Promises } from '../../../../base/common/async.js';
import Severity$1 from '../../../../base/common/severity.js';

let ExtensionDependencyChecker = class ExtensionDependencyChecker extends Disposable {
    constructor(
        extensionService,
        extensionsWorkbenchService,
        notificationService,
        hostService
    ) {
        super();
        this.extensionService = extensionService;
        this.extensionsWorkbenchService = extensionsWorkbenchService;
        this.notificationService = notificationService;
        this.hostService = hostService;
        CommandsRegistry.registerCommand(
            "workbench.extensions.installMissingDependencies",
            () => this.installMissingDependencies()
        );
        MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
            command: {
                id: "workbench.extensions.installMissingDependencies",
                category: ( localize(9214, "Extensions")),
                title: ( localize(9215, "Install Missing Dependencies"))
            }
        });
    }
    async getUninstalledMissingDependencies() {
        const allMissingDependencies = await this.getAllMissingDependencies();
        const localExtensions = await this.extensionsWorkbenchService.queryLocal();
        return allMissingDependencies.filter(id => localExtensions.every(l => !areSameExtensions(l.identifier, {
            id
        })));
    }
    async getAllMissingDependencies() {
        await this.extensionService.whenInstalledExtensionsRegistered();
        const runningExtensionsIds = this.extensionService.extensions.reduce((result, r) => {
            result.add(r.identifier.value.toLowerCase());
            return result;
        }, ( new Set()));
        const missingDependencies = ( new Set());
        for (const extension of this.extensionService.extensions) {
            if (extension.extensionDependencies) {
                extension.extensionDependencies.forEach(dep => {
                    if (!( runningExtensionsIds.has(dep.toLowerCase()))) {
                        missingDependencies.add(dep);
                    }
                });
            }
        }
        return [...( missingDependencies.values())];
    }
    async installMissingDependencies() {
        const missingDependencies = await this.getUninstalledMissingDependencies();
        if (missingDependencies.length) {
            const extensions = await this.extensionsWorkbenchService.getExtensions(( missingDependencies.map(id => ({
                id
            }))), CancellationToken.None);
            if (extensions.length) {
                await Promises.settled(( extensions.map(extension => this.extensionsWorkbenchService.install(extension))));
                this.notificationService.notify({
                    severity: Severity$1.Info,
                    message: ( localize(
                        9216,
                        "Finished installing missing dependencies. Please reload the window now."
                    )),
                    actions: {
                        primary: [( new Action("realod", ( localize(9217, "Reload Window")), "", true, () => this.hostService.reload()))]
                    }
                });
            }
        } else {
            this.notificationService.info(( localize(9218, "There are no missing dependencies to install.")));
        }
    }
};
ExtensionDependencyChecker = ( __decorate([( __param(0, IExtensionService)), ( __param(1, IExtensionsWorkbenchService)), ( __param(2, INotificationService)), ( __param(3, IHostService))], ExtensionDependencyChecker));

export { ExtensionDependencyChecker };
