import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { assertIsDefined } from '../../../../base/common/types.js';
import { isTextEditorViewState } from '../../../common/editor.js';
import { applyTextEditorOptions } from '../../../common/editor/editorOptions.js';
import { TextResourceEditorInput } from '../../../common/editor/textResourceEditorInput.js';
import { BaseTextEditorModel } from '../../../common/editor/textEditorModel.js';
import { UntitledTextEditorInput } from '../../../services/untitled/common/untitledTextEditorInput.js';
import { AbstractTextCodeEditor } from './textCodeEditor.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.js';
import { IStorageService } from '../../../../platform/storage/common/storage.js';
import { ITextResourceConfigurationService } from '../../../../editor/common/services/textResourceConfiguration.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.js';
import { IEditorGroupsService } from '../../../services/editor/common/editorGroupsService.js';
import { IEditorService } from '../../../services/editor/common/editorService.js';
import { IModelService } from '../../../../editor/common/services/model.js';
import { ILanguageService } from '../../../../editor/common/languages/language.js';
import { PLAINTEXT_LANGUAGE_ID } from '../../../../editor/common/languages/modesRegistry.js';
import { IFileService } from '../../../../platform/files/common/files.js';
var TextResourceEditor_1;
let AbstractTextResourceEditor = class AbstractTextResourceEditor extends AbstractTextCodeEditor {
    constructor(id, group, telemetryService, instantiationService, storageService, textResourceConfigurationService, themeService, editorGroupService, editorService, fileService) {
        super(id, group, telemetryService, instantiationService, storageService, textResourceConfigurationService, themeService, editorService, editorGroupService, fileService);
    }
    async setInput(input, options, context, token) {
        await super.setInput(input, options, context, token);
        const resolvedModel = await input.resolve();
        if (token.isCancellationRequested) {
            return undefined;
        }
        if (!(resolvedModel instanceof BaseTextEditorModel)) {
            throw new Error('Unable to open file as text');
        }
        const control = assertIsDefined(this.editorControl);
        const textEditorModel = resolvedModel.textEditorModel;
        control.setModel(textEditorModel);
        if (!isTextEditorViewState(options?.viewState)) {
            const editorViewState = this.loadEditorViewState(input, context);
            if (editorViewState) {
                if (options?.selection) {
                    editorViewState.cursorState = [];
                }
                control.restoreViewState(editorViewState);
            }
        }
        if (options) {
            applyTextEditorOptions(options, control, 1 );
        }
        control.updateOptions(this.getReadonlyConfiguration(resolvedModel.isReadonly()));
    }
    revealLastLine() {
        const control = this.editorControl;
        if (!control) {
            return;
        }
        const model = control.getModel();
        if (model) {
            const lastLine = model.getLineCount();
            control.revealPosition({ lineNumber: lastLine, column: model.getLineMaxColumn(lastLine) }, 0 );
        }
    }
    clearInput() {
        super.clearInput();
        this.editorControl?.setModel(null);
    }
    tracksEditorViewState(input) {
        return input instanceof UntitledTextEditorInput || input instanceof TextResourceEditorInput;
    }
};
AbstractTextResourceEditor = ( __decorate([
    ( __param(2, ITelemetryService)),
    ( __param(3, IInstantiationService)),
    ( __param(4, IStorageService)),
    ( __param(5, ITextResourceConfigurationService)),
    ( __param(6, IThemeService)),
    ( __param(7, IEditorGroupsService)),
    ( __param(8, IEditorService)),
    ( __param(9, IFileService))
], AbstractTextResourceEditor));
let TextResourceEditor = class TextResourceEditor extends AbstractTextResourceEditor {
    static { TextResourceEditor_1 = this; }
    static { this.ID = 'workbench.editors.textResourceEditor'; }
    constructor(group, telemetryService, instantiationService, storageService, textResourceConfigurationService, themeService, editorService, editorGroupService, modelService, languageService, fileService) {
        super(TextResourceEditor_1.ID, group, telemetryService, instantiationService, storageService, textResourceConfigurationService, themeService, editorGroupService, editorService, fileService);
        this.modelService = modelService;
        this.languageService = languageService;
    }
    createEditorControl(parent, configuration) {
        super.createEditorControl(parent, configuration);
        const control = this.editorControl;
        if (control) {
            this._register(control.onDidPaste(e => this.onDidEditorPaste(e, control)));
        }
    }
    onDidEditorPaste(e, codeEditor) {
        if (this.input instanceof UntitledTextEditorInput && this.input.hasLanguageSetExplicitly) {
            return;
        }
        if (e.range.startLineNumber !== 1 || e.range.startColumn !== 1) {
            return;
        }
        if (codeEditor.getOption(91 )) {
            return;
        }
        const textModel = codeEditor.getModel();
        if (!textModel) {
            return;
        }
        const pasteIsWholeContents = textModel.getLineCount() === e.range.endLineNumber && textModel.getLineMaxColumn(e.range.endLineNumber) === e.range.endColumn;
        if (!pasteIsWholeContents) {
            return;
        }
        const currentLanguageId = textModel.getLanguageId();
        if (currentLanguageId !== PLAINTEXT_LANGUAGE_ID) {
            return;
        }
        let candidateLanguage = undefined;
        if (e.languageId) {
            candidateLanguage = { id: e.languageId, source: 'event' };
        }
        else {
            const guess = this.languageService.guessLanguageIdByFilepathOrFirstLine(textModel.uri, textModel.getLineContent(1).substr(0, 1000 )) ?? undefined;
            if (guess) {
                candidateLanguage = { id: guess, source: 'guess' };
            }
        }
        if (candidateLanguage && candidateLanguage.id !== PLAINTEXT_LANGUAGE_ID) {
            if (this.input instanceof UntitledTextEditorInput && candidateLanguage.source === 'event') {
                this.input.setLanguageId(candidateLanguage.id);
            }
            else {
                textModel.setLanguage(this.languageService.createById(candidateLanguage.id));
            }
            const opts = this.modelService.getCreationOptions(textModel.getLanguageId(), textModel.uri, textModel.isForSimpleWidget);
            textModel.detectIndentation(opts.insertSpaces, opts.tabSize);
        }
    }
};
TextResourceEditor = TextResourceEditor_1 = ( __decorate([
    ( __param(1, ITelemetryService)),
    ( __param(2, IInstantiationService)),
    ( __param(3, IStorageService)),
    ( __param(4, ITextResourceConfigurationService)),
    ( __param(5, IThemeService)),
    ( __param(6, IEditorService)),
    ( __param(7, IEditorGroupsService)),
    ( __param(8, IModelService)),
    ( __param(9, ILanguageService)),
    ( __param(10, IFileService))
], TextResourceEditor));
export { AbstractTextResourceEditor, TextResourceEditor };
