"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadPreference = void 0;
exports.getPreferences = getPreferences;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_workspace_1 = require("@opensumi/ide-workspace");
const vscode_1 = require("../../../common/vscode");
function getPreferences(preferenceProviderProvider, rootFolders) {
    const folders = rootFolders.map((root) => root.uri.toString());
    return ide_core_browser_1.PreferenceScope.getScopes().reduce((result, scope) => {
        result[scope] = {};
        const provider = preferenceProviderProvider(scope);
        if (scope === ide_core_browser_1.PreferenceScope.Folder) {
            for (const f of folders) {
                const folderPrefs = provider.getPreferences(f);
                result[scope][f] = folderPrefs;
            }
        }
        else {
            result[scope] = provider.getPreferences();
            const languagePreferences = provider.getLanguagePreferences();
            if (languagePreferences) {
                Object.keys(languagePreferences).forEach((language) => {
                    result[scope][`[${language}]`] = languagePreferences[language];
                });
            }
        }
        return result;
    }, {});
}
let MainThreadPreference = class MainThreadPreference {
    constructor(rpcProtocol) {
        this.rpcProtocol = rpcProtocol;
        this.toDispose = new ide_core_browser_1.DisposableCollection();
        this.proxy = this.rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostPreference);
        this.toDispose.push(this.preferenceService.onPreferencesChanged((changes) => {
            const roots = this.workspaceService.tryGetRoots();
            const data = getPreferences(this.preferenceProviderProvider, roots);
            const eventData = [];
            for (const preferenceName of Object.keys(changes)) {
                const { newValue } = changes[preferenceName];
                eventData.push({ preferenceName, newValue });
            }
            this.proxy.$acceptConfigurationChanged(data, eventData);
        }));
        this.initializeConfiguration();
        this.toDispose.push(this.preferenceSchemaProvider.onDidPreferenceSchemaChanged(() => {
            this.initializeConfiguration();
        }));
    }
    dispose() {
        this.toDispose.dispose();
    }
    async initializeConfiguration() {
        const roots = this.workspaceService.tryGetRoots();
        const data = getPreferences(this.preferenceProviderProvider, roots);
        this.proxy.$initializeConfiguration(data);
    }
    async $updateConfigurationOption(target, key, value, resource) {
        const scope = this.parseConfigurationTarget(target);
        await this.preferenceService.set(key, value, scope, resource);
    }
    async $removeConfigurationOption(target, key, resource) {
        const scope = this.parseConfigurationTarget(target);
        await this.preferenceService.set(key, undefined, scope, resource);
    }
    /**
     * 装换VSCode ConfigurationTaregt到PreferenceService中的Scope
     * @param target
     */
    parseConfigurationTarget(target) {
        if (typeof target === 'boolean') {
            return target ? ide_core_browser_1.PreferenceScope.User : ide_core_browser_1.PreferenceScope.Workspace;
        }
        switch (target) {
            case vscode_1.ConfigurationTarget.Global:
                return ide_core_browser_1.PreferenceScope.User;
            case vscode_1.ConfigurationTarget.Workspace:
                return ide_core_browser_1.PreferenceScope.Workspace;
            case vscode_1.ConfigurationTarget.WorkspaceFolder:
                return ide_core_browser_1.PreferenceScope.Folder;
            default:
                return undefined;
        }
    }
};
exports.MainThreadPreference = MainThreadPreference;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.PreferenceService),
    tslib_1.__metadata("design:type", Object)
], MainThreadPreference.prototype, "preferenceService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.PreferenceProviderProvider),
    tslib_1.__metadata("design:type", Function)
], MainThreadPreference.prototype, "preferenceProviderProvider", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_browser_1.PreferenceSchemaProvider),
    tslib_1.__metadata("design:type", ide_core_browser_1.PreferenceSchemaProvider)
], MainThreadPreference.prototype, "preferenceSchemaProvider", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_workspace_1.IWorkspaceService),
    tslib_1.__metadata("design:type", Object)
], MainThreadPreference.prototype, "workspaceService", void 0);
exports.MainThreadPreference = MainThreadPreference = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(Symbol())),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadPreference);
//# sourceMappingURL=main.thread.preference.js.map