
import { __decorate, __param } from '../../../../../../../../external/tslib/tslib.es6.js';
import { localize } from '../../../../../nls.js';
import { BaseBinaryResourceEditor } from '../../../../browser/parts/editor/binaryEditor.js';
import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.service.js';
import { IThemeService } from '../../../../../platform/theme/common/themeService.service.js';
import { FileEditorInput } from './fileEditorInput.js';
import { BINARY_FILE_EDITOR_ID, BINARY_TEXT_FILE_MODE } from '../../common/files.js';
import { IStorageService } from '../../../../../platform/storage/common/storage.service.js';
import { EditorResolution } from '../../../../../platform/editor/common/editor.js';
import { ResolvedStatus } from '../../../../services/editor/common/editorResolverService.js';
import { IEditorResolverService } from '../../../../services/editor/common/editorResolverService.service.js';
import { isEditorInputWithOptions } from '../../../../common/editor.js';
import { DiffEditorInput } from '../../../../common/editor/diffEditorInput.js';

var BinaryFileEditor_1;
let BinaryFileEditor = class BinaryFileEditor extends BaseBinaryResourceEditor {
    static {
        BinaryFileEditor_1 = this;
    }
    static {
        this.ID = BINARY_FILE_EDITOR_ID;
    }
    constructor(
        group,
        telemetryService,
        themeService,
        editorResolverService,
        storageService
    ) {
        super(BinaryFileEditor_1.ID, group, {
            openInternal: (input, options) => this.openInternal(input, options)
        }, telemetryService, themeService, storageService);
        this.editorResolverService = editorResolverService;
    }
    async openInternal(input, options) {
        if (input instanceof FileEditorInput && this.group.activeEditor) {
            const activeEditor = this.group.activeEditor;
            const untypedActiveEditor = activeEditor?.toUntyped();
            if (!untypedActiveEditor) {
                return;
            }
            let resolvedEditor = await this.editorResolverService.resolveEditor({
                ...untypedActiveEditor,
                options: {
                    ...options,
                    override: EditorResolution.PICK
                }
            }, this.group);
            if (resolvedEditor === ResolvedStatus.NONE) {
                resolvedEditor = undefined;
            } else if (resolvedEditor === ResolvedStatus.ABORT) {
                return;
            }
            if (isEditorInputWithOptions(resolvedEditor)) {
                for (const editor of resolvedEditor.editor instanceof DiffEditorInput ? [resolvedEditor.editor.original, resolvedEditor.editor.modified] : [resolvedEditor.editor]) {
                    if (editor instanceof FileEditorInput) {
                        editor.setForceOpenAsText();
                        editor.setPreferredLanguageId(BINARY_TEXT_FILE_MODE);
                    }
                }
            }
            await this.group.replaceEditors([{
                editor: activeEditor,
                replacement: resolvedEditor?.editor ?? input,
                options: {
                    ...(resolvedEditor?.options ?? options)
                }
            }]);
        }
    }
    getTitle() {
        return this.input ? this.input.getName() : ( localize(10262, "Binary File Viewer"));
    }
};
BinaryFileEditor = BinaryFileEditor_1 = ( __decorate([( __param(1, ITelemetryService)), ( __param(2, IThemeService)), ( __param(3, IEditorResolverService)), ( __param(4, IStorageService))], BinaryFileEditor));

export { BinaryFileEditor };
