
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { ITextModelService } from '../../../../editor/common/services/resolverService.service.js';
import { ITextResourceConfigurationService } from '../../../../editor/common/services/textResourceConfiguration.service.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { IFileDialogService } from '../../../../platform/dialogs/common/dialogs.service.js';
import { IFileService } from '../../../../platform/files/common/files.service.js';
import { ILabelService } from '../../../../platform/label/common/label.service.js';
import { EditorInputCapabilities } from '../../../common/editor.js';
import { IInteractiveHistoryService } from '../../interactive/browser/interactiveHistoryService.service.js';
import { NotebookSetting, CellKind, CellEditType } from '../../notebook/common/notebookCommon.js';
import { NotebookEditorInput } from '../../notebook/common/notebookEditorInput.js';
import { INotebookEditorModelResolverService } from '../../notebook/common/notebookEditorModelResolverService.service.js';
import { INotebookService } from '../../notebook/common/notebookService.service.js';
import { ICustomEditorLabelService } from '../../../services/editor/common/customEditorLabelService.service.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.service.js';
import { IFilesConfigurationService } from '../../../services/filesConfiguration/common/filesConfigurationService.service.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { localize } from '../../../../nls.js';
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
import { IWorkbenchEnvironmentService } from '../../../services/environment/common/environmentService.service.js';
import { IPathService } from '../../../services/path/common/pathService.service.js';

var ReplEditorInput_1;
const replTabIcon = registerIcon("repl-editor-label-icon", Codicon.debugLineByLine, ( localize(10941, "Icon of the REPL editor label.")));
let ReplEditorInput = class ReplEditorInput extends NotebookEditorInput {
    static {
        ReplEditorInput_1 = this;
    }
    static {
        this.ID = "workbench.editorinputs.replEditorInput";
    }
    constructor(
        resource,
        label,
        _notebookService,
        _notebookModelResolverService,
        _fileDialogService,
        labelService,
        fileService,
        filesConfigurationService,
        extensionService,
        editorService,
        textResourceConfigurationService,
        customEditorLabelService,
        historyService,
        _textModelService,
        configurationService,
        environmentService,
        pathService
    ) {
        super(
            resource,
            undefined,
            "jupyter-notebook",
            {},
            _notebookService,
            _notebookModelResolverService,
            _fileDialogService,
            labelService,
            fileService,
            filesConfigurationService,
            extensionService,
            editorService,
            textResourceConfigurationService,
            customEditorLabelService,
            environmentService,
            pathService
        );
        this.historyService = historyService;
        this._textModelService = _textModelService;
        this.isDisposing = false;
        this.isScratchpad = resource.scheme === "untitled" && configurationService.getValue(NotebookSetting.InteractiveWindowPromptToSave) !== true;
        this.label = label ?? this.createEditorLabel(resource);
    }
    getIcon() {
        return replTabIcon;
    }
    createEditorLabel(resource) {
        if (!resource) {
            return "REPL";
        }
        if (resource.scheme === "untitled") {
            const match = ( new RegExp("Untitled-(\\d+).")).exec(resource.path);
            if (match?.length === 2) {
                return `REPL - ${match[1]}`;
            }
        }
        const filename = resource.path.split("/").pop();
        return filename ? `REPL - ${filename}` : "REPL";
    }
    get typeId() {
        return ReplEditorInput_1.ID;
    }
    get editorId() {
        return "repl";
    }
    getName() {
        return this.label;
    }
    get editorInputs() {
        return [this];
    }
    get capabilities() {
        const capabilities = super.capabilities;
        const scratchPad = this.isScratchpad ? EditorInputCapabilities.Scratchpad : 0;
        return capabilities | EditorInputCapabilities.Readonly | scratchPad;
    }
    async resolve() {
        const model = await super.resolve();
        if (model) {
            this.ensureInputBoxCell(model.notebook);
        }
        return model;
    }
    ensureInputBoxCell(notebook) {
        const lastCell = notebook.cells[notebook.cells.length - 1];
        if (!lastCell || lastCell.cellKind === CellKind.Markup || lastCell.outputs.length > 0 || lastCell.internalMetadata.executionOrder !== undefined) {
            notebook.applyEdits([{
                editType: CellEditType.Replace,
                index: notebook.cells.length,
                count: 0,
                cells: [{
                    cellKind: CellKind.Code,
                    language: "python",
                    mime: undefined,
                    outputs: [],
                    source: ""
                }]
            }], true, undefined, () => undefined, undefined, false);
        }
    }
    async resolveInput(notebook) {
        if (this.inputModelRef) {
            return this.inputModelRef.object.textEditorModel;
        }
        const lastCell = notebook.cells[notebook.cells.length - 1];
        if (!lastCell) {
            throw ( new Error("The REPL editor requires at least one cell for the input box."));
        }
        this.inputModelRef = await this._textModelService.createModelReference(lastCell.uri);
        return this.inputModelRef.object.textEditorModel;
    }
    dispose() {
        if (!this.isDisposing) {
            this.isDisposing = true;
            this.editorModelReference?.object.revert({
                soft: true
            });
            this.inputModelRef?.dispose();
            super.dispose();
        }
    }
};
ReplEditorInput = ReplEditorInput_1 = ( __decorate([( __param(2, INotebookService)), ( __param(3, INotebookEditorModelResolverService)), ( __param(4, IFileDialogService)), ( __param(5, ILabelService)), ( __param(6, IFileService)), ( __param(7, IFilesConfigurationService)), ( __param(8, IExtensionService)), ( __param(9, IEditorService)), ( __param(10, ITextResourceConfigurationService)), ( __param(11, ICustomEditorLabelService)), ( __param(12, IInteractiveHistoryService)), ( __param(13, ITextModelService)), ( __param(14, IConfigurationService)), ( __param(15, IWorkbenchEnvironmentService)), ( __param(16, IPathService))], ReplEditorInput));

export { ReplEditorInput };
