
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { localize } from '../../../../nls.js';
import { getLocation, parse } from '../../../../base/common/json.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { CompletionItemKind } from '../../../../editor/common/languages.js';
import { IExtensionManagementService } from '../../../../platform/extensionManagement/common/extensionManagement.service.js';
import { Range } from '../../../../editor/common/core/range.js';
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.service.js';

let ExtensionsCompletionItemsProvider = class ExtensionsCompletionItemsProvider extends Disposable {
    constructor(extensionManagementService, languageFeaturesService) {
        super();
        this.extensionManagementService = extensionManagementService;
        this._register(languageFeaturesService.completionProvider.register({
            language: "jsonc",
            pattern: "**/settings.json"
        }, {
            _debugDisplayName: "extensionsCompletionProvider",
            provideCompletionItems: async (model, position, _context, token) => {
                const getWordRangeAtPosition = (model, position) => {
                    const wordAtPosition = model.getWordAtPosition(position);
                    return wordAtPosition ? ( new Range(
                        position.lineNumber,
                        wordAtPosition.startColumn,
                        position.lineNumber,
                        wordAtPosition.endColumn
                    )) : null;
                };
                const location = getLocation(model.getValue(), model.getOffsetAt(position));
                const range = getWordRangeAtPosition(model, position) ?? Range.fromPositions(position, position);
                if (location.path[0] === "extensions.supportUntrustedWorkspaces" && location.path.length === 2 && location.isAtPropertyKey) {
                    let alreadyConfigured = [];
                    try {
                        alreadyConfigured = ( Object.keys(parse(model.getValue())["extensions.supportUntrustedWorkspaces"]));
                    } catch (e) {}
                    return {
                        suggestions: await this.provideSupportUntrustedWorkspacesExtensionProposals(alreadyConfigured, range)
                    };
                }
                return {
                    suggestions: []
                };
            }
        }));
    }
    async provideSupportUntrustedWorkspacesExtensionProposals(alreadyConfigured, range) {
        const suggestions = [];
        const installedExtensions = (await this.extensionManagementService.getInstalled()).filter(e => e.manifest.main);
        const proposedExtensions = installedExtensions.filter(e => alreadyConfigured.indexOf(e.identifier.id) === -1);
        if (proposedExtensions.length) {
            suggestions.push(...( proposedExtensions.map(e => {
                const text = `"${e.identifier.id}": {\n\t"supported": true,\n\t"version": "${e.manifest.version}"\n},`;
                return {
                    label: e.identifier.id,
                    kind: CompletionItemKind.Value,
                    insertText: text,
                    filterText: text,
                    range
                };
            })));
        } else {
            const text = "\"vscode.csharp\": {\n\t\"supported\": true,\n\t\"version\": \"0.0.0\"\n},";
            suggestions.push({
                label: ( localize(10028, "Example")),
                kind: CompletionItemKind.Value,
                insertText: text,
                filterText: text,
                range
            });
        }
        return suggestions;
    }
};
ExtensionsCompletionItemsProvider = ( __decorate([( __param(0, IExtensionManagementService)), ( __param(1, ILanguageFeaturesService))], ExtensionsCompletionItemsProvider));

export { ExtensionsCompletionItemsProvider };
