"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadCustomEditor = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const browser_1 = require("@opensumi/ide-editor/lib/browser");
const ide_webview_1 = require("@opensumi/ide-webview");
const vscode_1 = require("../../../common/vscode");
const custom_editor_1 = require("../../../common/vscode/custom-editor");
const main_thread_api_webview_1 = require("./main.thread.api.webview");
let MainThreadCustomEditor = class MainThreadCustomEditor extends ide_core_common_1.WithEventBus {
    constructor(protocol, webview) {
        super();
        this.webview = webview;
        this.customEditors = new Map();
        this.proxy = protocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostCustomEditor);
    }
    $acceptCustomDocumentDirty(uri, dirty) {
        this.eventBus.fire(new browser_1.ResourceDecorationNeedChangeEvent({
            uri: ide_core_common_1.URI.from(uri),
            decoration: {
                dirty,
            },
        }));
    }
    async onShouldSave(e) {
        const { viewType, uri, cancellationToken } = e.payload;
        const editor = this.customEditors.get(viewType);
        if (editor) {
            if (editor.type === vscode_1.CustomEditorType.TextEditor) {
                const docRef = this.editorDocumentModelService.getModelReference(uri);
                if (docRef) {
                    try {
                        await docRef.instance.save();
                    }
                    finally {
                        docRef.dispose();
                    }
                }
            }
            else if (editor.type === vscode_1.CustomEditorType.FullEditor) {
                return this.proxy.$saveCustomDocument(viewType, uri.codeUri, cancellationToken);
            }
        }
    }
    async onShouldRevert(e) {
        const { viewType, uri, cancellationToken } = e.payload;
        const editor = this.customEditors.get(viewType);
        if (editor) {
            if (editor.type === vscode_1.CustomEditorType.TextEditor) {
                const docRef = this.editorDocumentModelService.getModelReference(uri);
                if (docRef) {
                    try {
                        await docRef.instance.revert();
                    }
                    finally {
                        docRef.dispose();
                    }
                }
            }
            else if (editor.type === vscode_1.CustomEditorType.FullEditor) {
                return this.proxy.$revertCustomDocument(viewType, uri.codeUri, cancellationToken);
            }
        }
    }
    async onShouldEdit(e) {
        const { viewType, uri, type } = e.payload;
        const editor = this.customEditors.get(viewType);
        if (editor) {
            if (editor.type === vscode_1.CustomEditorType.TextEditor) {
                const docRef = this.editorDocumentModelService.getModelReference(uri);
                if (docRef) {
                    try {
                        if (type === 'undo') {
                            await docRef.instance.getMonacoModel().undo();
                        }
                        else {
                            await docRef.instance.getMonacoModel().redo();
                        }
                    }
                    finally {
                        docRef.dispose();
                    }
                }
            }
            else if (editor.type === vscode_1.CustomEditorType.FullEditor) {
                if (type === 'undo') {
                    await this.proxy.$undo(viewType, uri.codeUri);
                }
                else {
                    await this.proxy.$redo(viewType, uri.codeUri);
                }
            }
        }
    }
    async onCustomEditorShouldDisplayEvent(e) {
        const editor = this.customEditors.get(e.payload.viewType);
        if (editor) {
            const { viewType, uri, openTypeId, webviewPanelId, cancellationToken } = e.payload;
            if (editor.type === vscode_1.CustomEditorType.TextEditor) {
                const docRef = await this.editorDocumentModelService.createModelReference(uri);
                if (!docRef) {
                    throw new Error(`failed to open document ${uri} for custom editor`);
                    return;
                }
                if (cancellationToken.isCancellationRequested) {
                    docRef.dispose();
                    return;
                }
                const webview = this.webviewService.getWebview(webviewPanelId);
                if (!webview) {
                    docRef.dispose();
                    throw new Error(`failed to find webview ${webviewPanelId}`);
                }
                this.eventBus.on(custom_editor_1.CustomEditorShouldHideEvent, (e) => {
                    if (uri.isEqual(e.payload.uri)) {
                        docRef.dispose();
                    }
                });
                this.webview.pipeBrowserHostedWebviewPanel(webview, {
                    uri,
                    openTypeId,
                }, viewType, editor.options.webviewOptions || {}, editor.extensionInfo);
                this.proxy.$resolveCustomTextEditor(viewType, uri.codeUri, webviewPanelId, cancellationToken);
            }
            else {
                const webview = this.webviewService.getWebview(webviewPanelId);
                if (!webview) {
                    throw new Error(`failed to find webview ${webviewPanelId}`);
                }
                this.webview.pipeBrowserHostedWebviewPanel(webview, {
                    uri,
                    openTypeId,
                }, viewType, editor.options.webviewOptions || {}, editor.extensionInfo);
                this.proxy.$resolveCustomTextEditor(viewType, uri.codeUri, webviewPanelId, cancellationToken);
            }
        }
    }
    $registerCustomEditor(viewType, editorType, options = {}, extensionInfo) {
        this.customEditors.set(viewType, {
            type: editorType,
            options,
            extensionInfo,
        });
        this.eventBus.fire(new custom_editor_1.CustomEditorOptionChangeEvent({
            viewType,
            options,
        }));
    }
    $unregisterCustomEditor(viewType) {
        this.customEditors.delete(viewType);
    }
};
exports.MainThreadCustomEditor = MainThreadCustomEditor;
tslib_1.__decorate([
    (0, di_1.Autowired)(browser_1.IEditorDocumentModelService),
    tslib_1.__metadata("design:type", Object)
], MainThreadCustomEditor.prototype, "editorDocumentModelService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_webview_1.IWebviewService),
    tslib_1.__metadata("design:type", Object)
], MainThreadCustomEditor.prototype, "webviewService", void 0);
tslib_1.__decorate([
    (0, ide_core_common_1.OnEvent)(custom_editor_1.CustomEditorShouldSaveEvent),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [custom_editor_1.CustomEditorShouldSaveEvent]),
    tslib_1.__metadata("design:returntype", Promise)
], MainThreadCustomEditor.prototype, "onShouldSave", null);
tslib_1.__decorate([
    (0, ide_core_common_1.OnEvent)(custom_editor_1.CustomEditorShouldRevertEvent),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [custom_editor_1.CustomEditorShouldSaveEvent]),
    tslib_1.__metadata("design:returntype", Promise)
], MainThreadCustomEditor.prototype, "onShouldRevert", null);
tslib_1.__decorate([
    (0, ide_core_common_1.OnEvent)(custom_editor_1.CustomEditorShouldEditEvent),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [custom_editor_1.CustomEditorShouldEditEvent]),
    tslib_1.__metadata("design:returntype", Promise)
], MainThreadCustomEditor.prototype, "onShouldEdit", null);
tslib_1.__decorate([
    (0, ide_core_common_1.OnEvent)(custom_editor_1.CustomEditorShouldDisplayEvent),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [custom_editor_1.CustomEditorShouldDisplayEvent]),
    tslib_1.__metadata("design:returntype", Promise)
], MainThreadCustomEditor.prototype, "onCustomEditorShouldDisplayEvent", null);
exports.MainThreadCustomEditor = MainThreadCustomEditor = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__metadata("design:paramtypes", [Object, main_thread_api_webview_1.MainThreadWebview])
], MainThreadCustomEditor);
//# sourceMappingURL=main.thread.custom-editor.js.map