"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EditorBrowserContributionRunner = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_editor_1 = require("@opensumi/ide-editor");
const browser_1 = require("@opensumi/ide-editor/lib/browser");
const ide_main_layout_1 = require("@opensumi/ide-main-layout");
const ide_theme_1 = require("@opensumi/ide-theme");
const types_1 = require("../types");
let EditorBrowserContributionRunner = class EditorBrowserContributionRunner extends types_1.AbstractSumiBrowserContributionRunner {
    run(param) {
        const disposer = new ide_core_common_1.Disposable();
        if (this.contribution.editor) {
            this.contribution.editor.view.forEach((component) => {
                disposer.addDispose(this.registerEditorComponent(component, param));
            });
        }
        return disposer;
    }
    registerEditorComponent(viewContribution, runParam) {
        const disposer = new ide_core_common_1.Disposable();
        const { extendProtocol, extendService } = runParam.getExtensionExtendService(this.extension, viewContribution.id);
        const scheme = viewContribution.scheme || ide_core_common_1.Schemes.file;
        disposer.addDispose(this.editorComponentRegistry.registerEditorComponent({
            uid: viewContribution.id,
            scheme,
            component: viewContribution.component,
            renderMode: viewContribution.renderMode,
        }, {
            kaitianExtendService: extendService,
            kaitianExtendSet: extendProtocol,
            sumiExtendService: extendService,
            sumiExtendSet: extendProtocol,
        }));
        if (scheme === ide_core_common_1.Schemes.file) {
            disposer.addDispose(this.editorComponentRegistry.registerEditorComponentResolver(scheme, (resource, results) => {
                let shouldShow = false;
                // 旧fileExt处理
                if (viewContribution.fileExt && viewContribution.fileExt.indexOf(resource.uri.path.ext) > -1) {
                    if (!viewContribution.shouldPreview) {
                        return;
                    }
                    const shouldPreview = viewContribution.shouldPreview(resource.uri.path);
                    if (shouldPreview) {
                        shouldShow = true;
                    }
                }
                // handles处理
                if (viewContribution.handles) {
                    if (viewContribution.handles(resource.uri.codeUri)) {
                        shouldShow = true;
                    }
                }
                if (shouldShow) {
                    results.push({
                        type: browser_1.EditorOpenType.component,
                        componentId: viewContribution.id,
                        title: viewContribution.title || '预览',
                        weight: viewContribution.priority || 10,
                    });
                }
            }));
        }
        else {
            disposer.addDispose(this.editorComponentRegistry.registerEditorComponentResolver(scheme, (resource, results) => {
                if (viewContribution.handles) {
                    if (!viewContribution.handles(resource.uri.codeUri)) {
                        return;
                    }
                }
                results.push({
                    type: browser_1.EditorOpenType.component,
                    componentId: viewContribution.id,
                    title: viewContribution.title || viewContribution.id,
                    weight: viewContribution.priority || 10,
                });
            }));
        }
        this.resourceService.registerResourceProvider({
            scheme,
            provideResource: (uri) => ({
                uri,
                name: viewContribution.tabTitle || viewContribution.id,
                icon: viewContribution.tabIconPath
                    ? this.iconService.fromIcon(this.extension.path, viewContribution.tabIconPath, ide_theme_1.IconType.Background)
                    : '',
            }),
        });
        return disposer;
    }
};
exports.EditorBrowserContributionRunner = EditorBrowserContributionRunner;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_main_layout_1.IMainLayoutService),
    tslib_1.__metadata("design:type", Object)
], EditorBrowserContributionRunner.prototype, "layoutService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_editor_1.ResourceService),
    tslib_1.__metadata("design:type", ide_editor_1.ResourceService)
], EditorBrowserContributionRunner.prototype, "resourceService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(browser_1.EditorComponentRegistry),
    tslib_1.__metadata("design:type", browser_1.EditorComponentRegistry)
], EditorBrowserContributionRunner.prototype, "editorComponentRegistry", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], EditorBrowserContributionRunner.prototype, "iconService", void 0);
exports.EditorBrowserContributionRunner = EditorBrowserContributionRunner = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true })
], EditorBrowserContributionRunner);
//# sourceMappingURL=editor.js.map