"use strict";
// *****************************************************************************
// Copyright (C) 2021 SAP SE or an SAP affiliate company and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginCustomEditorRegistry = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("@theia/core/shared/inversify");
const disposable_1 = require("@theia/core/lib/common/disposable");
const custom_editor_opener_1 = require("./custom-editor-opener");
const core_1 = require("@theia/core");
const browser_1 = require("@theia/core/lib/browser");
const custom_editor_widget_1 = require("./custom-editor-widget");
let PluginCustomEditorRegistry = class PluginCustomEditorRegistry {
    constructor() {
        this.editors = new Map();
        this.pendingEditors = new Set();
        this.resolvers = new Map();
        this.onWillOpenCustomEditorEmitter = new core_1.Emitter();
        this.onWillOpenCustomEditor = this.onWillOpenCustomEditorEmitter.event;
        this.resolveWidget = (widget, options) => {
            const resolver = this.resolvers.get(widget.viewType);
            if (resolver) {
                resolver(widget, options);
            }
            else {
                this.pendingEditors.add(widget);
                this.onWillOpenCustomEditorEmitter.fire(widget.viewType);
            }
        };
    }
    init() {
        this.widgetManager.onDidCreateWidget(({ factoryId, widget }) => {
            if (factoryId === custom_editor_widget_1.CustomEditorWidget.FACTORY_ID && widget instanceof custom_editor_widget_1.CustomEditorWidget) {
                const restoreState = widget.restoreState.bind(widget);
                widget.restoreState = state => {
                    if (state.viewType && state.strResource) {
                        restoreState(state);
                        this.resolveWidget(widget);
                    }
                    else {
                        widget.dispose();
                    }
                };
            }
        });
    }
    registerCustomEditor(editor, plugin) {
        if (this.editors.has(editor.viewType)) {
            console.warn('editor with such id already registered: ', JSON.stringify(editor));
            return disposable_1.Disposable.NULL;
        }
        this.editors.set(editor.viewType, editor);
        const toDispose = new disposable_1.DisposableCollection();
        toDispose.push(disposable_1.Disposable.create(() => this.editors.delete(editor.viewType)));
        const editorOpenHandler = new custom_editor_opener_1.CustomEditorOpener(editor, this.shell, this.widgetManager);
        toDispose.push(this.defaultOpenerService.addHandler(editorOpenHandler));
        toDispose.push(this.openWithService.registerHandler({
            id: editor.viewType,
            label: editorOpenHandler.label,
            providerName: plugin.metadata.model.displayName,
            canHandle: uri => editorOpenHandler.canHandle(uri),
            open: uri => editorOpenHandler.open(uri)
        }));
        toDispose.push(editorOpenHandler.onDidOpenCustomEditor(event => this.resolveWidget(event[0], event[1])));
        return toDispose;
    }
    registerResolver(viewType, resolver) {
        if (this.resolvers.has(viewType)) {
            throw new Error(`Resolver for ${viewType} already registered`);
        }
        for (const editorWidget of this.pendingEditors) {
            if (editorWidget.viewType === viewType) {
                resolver(editorWidget);
                this.pendingEditors.delete(editorWidget);
            }
        }
        this.resolvers.set(viewType, resolver);
        return disposable_1.Disposable.create(() => this.resolvers.delete(viewType));
    }
};
(0, tslib_1.__decorate)([
    (0, inversify_1.inject)(browser_1.DefaultOpenerService),
    (0, tslib_1.__metadata)("design:type", browser_1.DefaultOpenerService)
], PluginCustomEditorRegistry.prototype, "defaultOpenerService", void 0);
(0, tslib_1.__decorate)([
    (0, inversify_1.inject)(browser_1.WidgetManager),
    (0, tslib_1.__metadata)("design:type", browser_1.WidgetManager)
], PluginCustomEditorRegistry.prototype, "widgetManager", void 0);
(0, tslib_1.__decorate)([
    (0, inversify_1.inject)(browser_1.ApplicationShell),
    (0, tslib_1.__metadata)("design:type", browser_1.ApplicationShell)
], PluginCustomEditorRegistry.prototype, "shell", void 0);
(0, tslib_1.__decorate)([
    (0, inversify_1.inject)(browser_1.OpenWithService),
    (0, tslib_1.__metadata)("design:type", browser_1.OpenWithService)
], PluginCustomEditorRegistry.prototype, "openWithService", void 0);
(0, tslib_1.__decorate)([
    (0, inversify_1.postConstruct)(),
    (0, tslib_1.__metadata)("design:type", Function),
    (0, tslib_1.__metadata)("design:paramtypes", []),
    (0, tslib_1.__metadata)("design:returntype", void 0)
], PluginCustomEditorRegistry.prototype, "init", null);
PluginCustomEditorRegistry = (0, tslib_1.__decorate)([
    (0, inversify_1.injectable)()
], PluginCustomEditorRegistry);
exports.PluginCustomEditorRegistry = PluginCustomEditorRegistry;
//# sourceMappingURL=plugin-custom-editor-registry.js.map