"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigurationContributionPoint = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const common_1 = require("../../../common");
const types_1 = require("../../types");
const localization_1 = require("./localization");
let ConfigurationContributionPoint = class ConfigurationContributionPoint extends common_1.VSCodeContributePoint {
    async contribute() {
        //  当语言包插件被使用的时候，需要等待LocalizationsContributionPoint执行完成。否则配置项的多语言初始化的还是默认语言（英文）。
        if (this.localizationsContributionPoint.hasUncontributedPoint()) {
            try {
                await this.localizationsContributionPoint.whenContributed;
            }
            catch (error) {
                this.logger.error('Failed to wait contribute localizations.', error);
            }
        }
        for (const contrib of this.contributesMap) {
            const { extensionId, contributes } = contrib;
            const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
            if (!extension) {
                continue;
            }
            // 一个插件可以注册多个 preference 类别
            let configurations = contributes;
            if (!Array.isArray(configurations)) {
                configurations = [configurations];
            }
            const sections = [];
            for (const configuration of configurations) {
                if (configuration && configuration.properties) {
                    const tmpProperties = {};
                    for (const prop of Object.keys(configuration.properties)) {
                        const originalConfiguration = configuration.properties[prop];
                        tmpProperties[prop] = originalConfiguration;
                        if (originalConfiguration.description) {
                            tmpProperties[prop].description = this.getLocalizeFromNlsJSON(originalConfiguration.description, extensionId);
                        }
                        if (originalConfiguration.enumDescriptions) {
                            tmpProperties[prop].enumDescriptions = originalConfiguration.enumDescriptions.map((v) => this.getLocalizeFromNlsJSON(v, extensionId));
                        }
                        if (originalConfiguration.markdownDescription) {
                            tmpProperties[prop].markdownDescription = this.getLocalizeFromNlsJSON(originalConfiguration.markdownDescription, extensionId);
                        }
                    }
                    configuration.properties = tmpProperties;
                    configuration.title = this.getLocalizeFromNlsJSON(configuration.title, extensionId) || configuration.title;
                    this.updateConfigurationSchema(configuration);
                    sections.push({
                        title: configuration.title,
                        extensionId,
                        preferences: Object.keys(configuration.properties).map((v) => ({
                            id: v,
                        })),
                    });
                }
            }
            // 如果注册了多个 section, 注册为 subSections
            if (sections.length === 1) {
                const section = sections[0];
                this.addDispose(this.preferenceSettingsService.registerSettingSection('extension', section));
            }
            else if (sections.length > 1) {
                this.addDispose(this.preferenceSettingsService.registerSettingSection('extension', {
                    title: this.getLocalizeFromNlsJSON(extension.packageJSON.displayName, extensionId) ||
                        extension.packageJSON.displayName,
                    subSections: sections,
                }));
            }
        }
    }
    updateConfigurationSchema(schema) {
        this.validateConfigurationSchema(schema);
        this.addDispose(this.preferenceSchemaProvider.setSchema(schema));
    }
    validateConfigurationSchema(schema) {
        // eslint-disable-next-line guard-for-in
        for (const p in schema.properties) {
            const property = schema.properties[p];
            if (property.type === 'string[]') {
                property.type = 'array';
            }
            if (property.type !== 'object') {
                continue;
            }
            if (typeof property.default === 'undefined') {
                this.validateDefaultValue(property);
            }
            const properties = property.properties;
            if (properties) {
                for (const key in properties) {
                    if (typeof properties[key] !== 'object') {
                        delete properties[key];
                    }
                }
            }
        }
    }
    validateDefaultValue(property) {
        property.default = {};
        const properties = property.properties;
        if (properties) {
            for (const key in properties) {
                if (properties[key].default) {
                    property.default[key] = properties[key].default;
                    delete properties[key].default;
                }
            }
        }
    }
};
exports.ConfigurationContributionPoint = ConfigurationContributionPoint;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.PreferenceSchemaProvider),
    tslib_1.__metadata("design:type", ide_core_browser_1.PreferenceSchemaProvider)
], ConfigurationContributionPoint.prototype, "preferenceSchemaProvider", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.IPreferenceSettingsService),
    tslib_1.__metadata("design:type", Object)
], ConfigurationContributionPoint.prototype, "preferenceSettingsService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.PreferenceService),
    tslib_1.__metadata("design:type", Object)
], ConfigurationContributionPoint.prototype, "preferenceService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], ConfigurationContributionPoint.prototype, "extensionManageService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(localization_1.LocalizationsContributionPoint),
    tslib_1.__metadata("design:type", localization_1.LocalizationsContributionPoint)
], ConfigurationContributionPoint.prototype, "localizationsContributionPoint", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], ConfigurationContributionPoint.prototype, "logger", void 0);
exports.ConfigurationContributionPoint = ConfigurationContributionPoint = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('configuration'),
    (0, common_1.LifeCycle)(2 /* LifeCyclePhase.Initialize */)
], ConfigurationContributionPoint);
//# sourceMappingURL=configuration.js.map