
import { registerCss } from '../../../../../../../css.js';
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import * as interactive from './media/interactive.css';
import { $, append, Dimension } from '../../../../base/browser/dom.js';
import { createStyleSheet } from '../../../../base/browser/domStylesheets.js';
import { Emitter, Event } from '../../../../base/common/event.js';
import { DisposableStore, MutableDisposable } from '../../../../base/common/lifecycle.js';
import { CodeEditorWidget } from '../../../../editor/browser/widget/codeEditor/codeEditorWidget.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { EditorPane } from '../../../browser/parts/editor/editorPane.js';
import { EditorPaneSelectionChangeReason } from '../../../common/editor.js';
import { getSimpleEditorOptions } from '../../codeEditor/browser/simpleEditorOptions.js';
import { NotebookEditorExtensionsRegistry } from '../../notebook/browser/notebookEditorExtensions.js';
import { INotebookEditorService } from '../../notebook/browser/services/notebookEditorService.service.js';
import { getDefaultNotebookCreationOptions, NotebookEditorWidget } from '../../notebook/browser/notebookEditorWidget.js';
import { GroupsOrder } from '../../../services/editor/common/editorGroupsService.js';
import { IEditorGroupsService } from '../../../services/editor/common/editorGroupsService.service.js';
import { ExecutionStateCellStatusBarContrib, TimerCellStatusBarContrib } from '../../notebook/browser/contrib/cellStatusBar/executionStatusBarItemController.js';
import { INotebookKernelService } from '../../notebook/common/notebookKernelService.service.js';
import { ILanguageService } from '../../../../editor/common/languages/language.service.js';
import { MenuId } from '../../../../platform/actions/common/actions.js';
import { IMenuService } from '../../../../platform/actions/common/actions.service.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { INTERACTIVE_INPUT_CURSOR_BOUNDARY, ReplEditorSettings } from '../../interactive/browser/interactiveCommon.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { NotebookOptions } from '../../notebook/browser/notebookOptions.js';
import { ToolBar } from '../../../../base/browser/ui/toolbar/toolbar.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { createActionViewItem, getActionBarActions } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { EditorExtensionsRegistry } from '../../../../editor/browser/editorExtensions.js';
import { SelectionClipboardContributionID } from '../../codeEditor/browser/selectionClipboard.js';
import { ContextMenuController } from '../../../../editor/contrib/contextmenu/browser/contextmenu.js';
import { SuggestController } from '../../../../editor/contrib/suggest/browser/suggestController.js';
import { MarkerController } from '../../../../editor/contrib/gotoError/browser/gotoError.js';
import { ITextResourceConfigurationService } from '../../../../editor/common/services/textResourceConfiguration.service.js';
import { TextEditorSelectionSource } from '../../../../platform/editor/common/editor.js';
import { NotebookExecutionType } from '../../notebook/common/notebookExecutionStateService.js';
import { INotebookExecutionStateService } from '../../notebook/common/notebookExecutionStateService.service.js';
import { NOTEBOOK_KERNEL } from '../../notebook/common/notebookContextKeys.js';
import { IExtensionService } from '../../../services/extensions/common/extensions.service.js';
import { isEqual } from '../../../../base/common/resources.js';
import { NotebookFindContrib } from '../../notebook/browser/contrib/find/notebookFindWidget.js';
import { REPL_EDITOR_ID } from '../../notebook/common/notebookCommon.js';
import * as interactiveEditor from './interactiveEditor.css';
import { deepClone } from '../../../../base/common/objects.js';
import { GlyphHoverController } from '../../../../editor/contrib/hover/browser/glyphHoverController.js';
import { ContentHoverController } from '../../../../editor/contrib/hover/browser/contentHoverController.js';
import { ReplEditorInput } from './replEditorInput.js';
import { ReplInputHintContentWidget } from '../../interactive/browser/replInputHintContentWidget.js';
import { ServiceCollection } from '../../../../platform/instantiation/common/serviceCollection.js';
import { localize } from '../../../../nls.js';
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.service.js';

registerCss(interactive);
registerCss(interactiveEditor);
const INTERACTIVE_EDITOR_VIEW_STATE_PREFERENCE_KEY = "InteractiveEditorViewState";
const INPUT_CELL_VERTICAL_PADDING = 8;
const INPUT_CELL_HORIZONTAL_PADDING_RIGHT = 10;
const INPUT_EDITOR_PADDING = 8;
let ReplEditor = class ReplEditor extends EditorPane {
    get onDidFocus() {
        return this._onDidFocusWidget.event;
    }
    constructor(
        group,
        telemetryService,
        themeService,
        storageService,
        instantiationService,
        notebookWidgetService,
        contextKeyService,
        notebookKernelService,
        languageService,
        keybindingService,
        configurationService,
        menuService,
        contextMenuService,
        editorGroupService,
        textResourceConfigurationService,
        notebookExecutionStateService,
        extensionService,
        _accessibilityService
    ) {
        super(REPL_EDITOR_ID, group, telemetryService, themeService, storageService);
        this._accessibilityService = _accessibilityService;
        this._notebookWidget = {
            value: undefined
        };
        this._widgetDisposableStore = this._register(( new DisposableStore()));
        this._groupListener = this._register(( new MutableDisposable()));
        this._onDidFocusWidget = this._register(( new Emitter()));
        this._onDidChangeSelection = this._register(( new Emitter()));
        this.onDidChangeSelection = this._onDidChangeSelection.event;
        this._onDidChangeScroll = this._register(( new Emitter()));
        this.onDidChangeScroll = this._onDidChangeScroll.event;
        this._notebookWidgetService = notebookWidgetService;
        this._configurationService = configurationService;
        this._notebookKernelService = notebookKernelService;
        this._languageService = languageService;
        this._keybindingService = keybindingService;
        this._menuService = menuService;
        this._contextMenuService = contextMenuService;
        this._editorGroupService = editorGroupService;
        this._extensionService = extensionService;
        this._rootElement = $(".interactive-editor");
        this._contextKeyService = this._register(contextKeyService.createScoped(this._rootElement));
        this._contextKeyService.createKey("isCompositeNotebook", true);
        this._instantiationService = this._register(instantiationService.createChild(( new ServiceCollection([IContextKeyService, this._contextKeyService]))));
        this._editorOptions = this._computeEditorOptions();
        this._register(this._configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration("editor") || e.affectsConfiguration("notebook")) {
                this._editorOptions = this._computeEditorOptions();
            }
        }));
        this._notebookOptions = instantiationService.createInstance(NotebookOptions, this.window, true, {
            cellToolbarInteraction: "hover",
            globalToolbar: true,
            stickyScrollEnabled: false,
            dragAndDropEnabled: false,
            disableRulers: true
        });
        this._editorMemento = this.getEditorMemento(
            editorGroupService,
            textResourceConfigurationService,
            INTERACTIVE_EDITOR_VIEW_STATE_PREFERENCE_KEY
        );
        this._register(
            this._keybindingService.onDidUpdateKeybindings(this._updateInputHint, this)
        );
        this._register(notebookExecutionStateService.onDidChangeExecution(e => {
            if (e.type === NotebookExecutionType.cell && isEqual(e.notebook, this._notebookWidget.value?.viewModel?.notebookDocument.uri)) {
                const cell = this._notebookWidget.value?.getCellByHandle(e.cellHandle);
                if (cell && e.changed?.state) {
                    this._scrollIfNecessary(cell);
                }
            }
        }));
    }
    get inputCellContainerHeight() {
        return 19 + 2 + INPUT_CELL_VERTICAL_PADDING * 2 + INPUT_EDITOR_PADDING * 2;
    }
    get inputCellEditorHeight() {
        return 19 + INPUT_EDITOR_PADDING * 2;
    }
    createEditor(parent) {
        append(parent, this._rootElement);
        this._rootElement.style.position = "relative";
        this._notebookEditorContainer = append(this._rootElement, $(".notebook-editor-container"));
        this._inputCellContainer = append(this._rootElement, $(".input-cell-container"));
        this._inputCellContainer.style.position = "absolute";
        this._inputCellContainer.style.height = `${this.inputCellContainerHeight}px`;
        this._inputFocusIndicator = append(this._inputCellContainer, $(".input-focus-indicator"));
        this._inputRunButtonContainer = append(this._inputCellContainer, $(".run-button-container"));
        this._setupRunButtonToolbar(this._inputRunButtonContainer);
        this._inputEditorContainer = append(this._inputCellContainer, $(".input-editor-container"));
        this._createLayoutStyles();
    }
    _setupRunButtonToolbar(runButtonContainer) {
        const menu = this._register(
            this._menuService.createMenu(MenuId.ReplInputExecute, this._contextKeyService)
        );
        this._runbuttonToolbar = this._register(( new ToolBar(runButtonContainer, this._contextMenuService, {
            getKeyBinding: action => this._keybindingService.lookupKeybinding(action.id),
            actionViewItemProvider: (action, options) => {
                return createActionViewItem(this._instantiationService, action, options);
            },
            renderDropdownAsChildElement: true
        })));
        const {
            primary,
            secondary
        } = getActionBarActions(menu.getActions({
            shouldForwardArgs: true
        }));
        this._runbuttonToolbar.setActions([...primary, ...secondary]);
    }
    _createLayoutStyles() {
        this._styleElement = createStyleSheet(this._rootElement);
        const styleSheets = [];
        const {
            codeCellLeftMargin,
            cellRunGutter
        } = this._notebookOptions.getLayoutConfiguration();
        const {
            focusIndicator
        } = this._notebookOptions.getDisplayOptions();
        const leftMargin = this._notebookOptions.getCellEditorContainerLeftMargin();
        styleSheets.push(`
            .interactive-editor .input-cell-container {
                padding: ${INPUT_CELL_VERTICAL_PADDING}px ${INPUT_CELL_HORIZONTAL_PADDING_RIGHT}px ${INPUT_CELL_VERTICAL_PADDING}px ${leftMargin}px;
            }
        `);
        if (focusIndicator === "gutter") {
            styleSheets.push(`
                .interactive-editor .input-cell-container:focus-within .input-focus-indicator::before {
                    border-color: var(--vscode-notebook-focusedCellBorder) !important;
                }
                .interactive-editor .input-focus-indicator::before {
                    border-color: var(--vscode-notebook-inactiveFocusedCellBorder) !important;
                }
                .interactive-editor .input-cell-container .input-focus-indicator {
                    display: block;
                    top: ${INPUT_CELL_VERTICAL_PADDING}px;
                }
                .interactive-editor .input-cell-container {
                    border-top: 1px solid var(--vscode-notebook-inactiveFocusedCellBorder);
                }
            `);
        } else {
            styleSheets.push(`
                .interactive-editor .input-cell-container {
                    border-top: 1px solid var(--vscode-notebook-inactiveFocusedCellBorder);
                }
                .interactive-editor .input-cell-container .input-focus-indicator {
                    display: none;
                }
            `);
        }
        styleSheets.push(`
            .interactive-editor .input-cell-container .run-button-container {
                width: ${cellRunGutter}px;
                left: ${codeCellLeftMargin}px;
                margin-top: ${INPUT_EDITOR_PADDING - 2}px;
            }
        `);
        this._styleElement.textContent = styleSheets.join("\n");
    }
    _computeEditorOptions() {
        let overrideIdentifier = undefined;
        if (this._codeEditorWidget) {
            overrideIdentifier = this._codeEditorWidget.getModel()?.getLanguageId();
        }
        const editorOptions = deepClone(this._configurationService.getValue("editor", {
            overrideIdentifier
        }));
        const editorOptionsOverride = getSimpleEditorOptions(this._configurationService);
        const computed = ( Object.freeze({
            ...editorOptions,
            ...editorOptionsOverride,
            ...{
                ariaLabel: ( localize(10927, "REPL Input")),
                glyphMargin: true,
                padding: {
                    top: INPUT_EDITOR_PADDING,
                    bottom: INPUT_EDITOR_PADDING
                },
                hover: {
                    enabled: "on"
                },
                rulers: []
            }
        }));
        return computed;
    }
    saveState() {
        this._saveEditorViewState(this.input);
        super.saveState();
    }
    getViewState() {
        const input = this.input;
        if (!(input instanceof ReplEditorInput)) {
            return undefined;
        }
        this._saveEditorViewState(input);
        return this._loadNotebookEditorViewState(input);
    }
    _saveEditorViewState(input) {
        if (this._notebookWidget.value && input instanceof ReplEditorInput) {
            if (this._notebookWidget.value.isDisposed) {
                return;
            }
            const state = this._notebookWidget.value.getEditorViewState();
            const editorState = this._codeEditorWidget.saveViewState();
            this._editorMemento.saveEditorState(this.group, input.resource, {
                notebook: state,
                input: editorState
            });
        }
    }
    _loadNotebookEditorViewState(input) {
        const result = this._editorMemento.loadEditorState(this.group, input.resource);
        if (result) {
            return result;
        }
        for (const group of this._editorGroupService.getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE)) {
            if (group.activeEditorPane !== this && group.activeEditorPane === this && group.activeEditor?.matches(input)) {
                const notebook = this._notebookWidget.value?.getEditorViewState();
                const input = this._codeEditorWidget.saveViewState();
                return {
                    notebook,
                    input
                };
            }
        }
        return;
    }
    async setInput(input, options, context, token) {
        this._notebookWidget.value?.onWillHide();
        this._codeEditorWidget?.dispose();
        this._widgetDisposableStore.clear();
        this._notebookWidget = this._instantiationService.invokeFunction(this._notebookWidgetService.retrieveWidget, this.group.id, input, {
            isReplHistory: true,
            isReadOnly: true,
            contributions: NotebookEditorExtensionsRegistry.getSomeEditorContributions([
                ExecutionStateCellStatusBarContrib.id,
                TimerCellStatusBarContrib.id,
                NotebookFindContrib.id
            ]),
            menuIds: {
                notebookToolbar: MenuId.InteractiveToolbar,
                cellTitleToolbar: MenuId.InteractiveCellTitle,
                cellDeleteToolbar: MenuId.InteractiveCellDelete,
                cellInsertToolbar: MenuId.NotebookCellBetween,
                cellTopInsertToolbar: MenuId.NotebookCellListTop,
                cellExecuteToolbar: MenuId.InteractiveCellExecute,
                cellExecutePrimary: undefined
            },
            cellEditorContributions: EditorExtensionsRegistry.getSomeEditorContributions([
                SelectionClipboardContributionID,
                ContextMenuController.ID,
                ContentHoverController.ID,
                GlyphHoverController.ID,
                MarkerController.ID
            ]),
            options: this._notebookOptions,
            codeWindow: this.window
        }, undefined, this.window);
        const skipContributions = [
            "workbench.notebook.cellToolbar",
            "editor.contrib.inlineCompletionsController"
        ];
        const inputContributions = getDefaultNotebookCreationOptions().cellEditorContributions?.filter(c => skipContributions.indexOf(c.id) === -1);
        this._codeEditorWidget = this._instantiationService.createInstance(CodeEditorWidget, this._inputEditorContainer, this._editorOptions, {
            ...{
                isSimpleWidget: false,
                contributions: inputContributions
            }
        });
        if (this._lastLayoutDimensions) {
            this._notebookEditorContainer.style.height = `${this._lastLayoutDimensions.dimension.height - this.inputCellContainerHeight}px`;
            this._notebookWidget.value.layout(new Dimension(
                this._lastLayoutDimensions.dimension.width,
                this._lastLayoutDimensions.dimension.height - this.inputCellContainerHeight
            ), this._notebookEditorContainer);
            const leftMargin = this._notebookOptions.getCellEditorContainerLeftMargin();
            const maxHeight = Math.min(
                this._lastLayoutDimensions.dimension.height / 2,
                this.inputCellEditorHeight
            );
            this._codeEditorWidget.layout(this._validateDimension(
                this._lastLayoutDimensions.dimension.width - leftMargin - INPUT_CELL_HORIZONTAL_PADDING_RIGHT,
                maxHeight
            ));
            this._inputFocusIndicator.style.height = `${this.inputCellEditorHeight}px`;
            this._inputCellContainer.style.top = `${this._lastLayoutDimensions.dimension.height - this.inputCellContainerHeight}px`;
            this._inputCellContainer.style.width = `${this._lastLayoutDimensions.dimension.width}px`;
        }
        await super.setInput(input, options, context, token);
        const model = await input.resolve();
        if (this._runbuttonToolbar) {
            this._runbuttonToolbar.context = input.resource;
        }
        if (model === null) {
            throw ( new Error("The REPL model could not be resolved"));
        }
        this._notebookWidget.value?.setParentContextKeyService(this._contextKeyService);
        const viewState = options?.viewState ?? this._loadNotebookEditorViewState(input);
        await this._extensionService.whenInstalledExtensionsRegistered();
        await this._notebookWidget.value.setModel(model.notebook, viewState?.notebook, undefined, "repl");
        model.notebook.setCellCollapseDefault(this._notebookOptions.getCellCollapseDefault());
        this._notebookWidget.value.setOptions({
            isReadOnly: true
        });
        this._widgetDisposableStore.add(this._notebookWidget.value.onDidResizeOutput(cvm => {
            this._scrollIfNecessary(cvm);
        }));
        this._widgetDisposableStore.add(
            this._notebookWidget.value.onDidFocusWidget(() => this._onDidFocusWidget.fire())
        );
        this._widgetDisposableStore.add(this._notebookOptions.onDidChangeOptions(e => {
            if (e.compactView || e.focusIndicator) {
                this._styleElement?.remove();
                this._createLayoutStyles();
            }
            if (this._lastLayoutDimensions && this.isVisible()) {
                this.layout(this._lastLayoutDimensions.dimension, this._lastLayoutDimensions.position);
            }
            if (e.interactiveWindowCollapseCodeCells) {
                model.notebook.setCellCollapseDefault(this._notebookOptions.getCellCollapseDefault());
            }
        }));
        const editorModel = await input.resolveInput(model.notebook);
        this._codeEditorWidget.setModel(editorModel);
        if (viewState?.input) {
            this._codeEditorWidget.restoreViewState(viewState.input);
        }
        this._editorOptions = this._computeEditorOptions();
        this._codeEditorWidget.updateOptions(this._editorOptions);
        this._widgetDisposableStore.add(
            this._codeEditorWidget.onDidFocusEditorWidget(() => this._onDidFocusWidget.fire())
        );
        this._widgetDisposableStore.add(this._codeEditorWidget.onDidContentSizeChange(e => {
            if (!e.contentHeightChanged) {
                return;
            }
            if (this._lastLayoutDimensions) {
                this._layoutWidgets(this._lastLayoutDimensions.dimension, this._lastLayoutDimensions.position);
            }
        }));
        this._widgetDisposableStore.add(
            this._codeEditorWidget.onDidChangeCursorPosition(e => this._onDidChangeSelection.fire({
                reason: this._toEditorPaneSelectionChangeReason(e)
            }))
        );
        this._widgetDisposableStore.add(
            this._codeEditorWidget.onDidChangeModelContent(() => this._onDidChangeSelection.fire({
                reason: EditorPaneSelectionChangeReason.EDIT
            }))
        );
        this._widgetDisposableStore.add(
            this._notebookKernelService.onDidChangeNotebookAffinity(this._syncWithKernel, this)
        );
        this._widgetDisposableStore.add(
            this._notebookKernelService.onDidChangeSelectedNotebooks(this._syncWithKernel, this)
        );
        this._widgetDisposableStore.add(this.themeService.onDidColorThemeChange(() => {
            if (this.isVisible()) {
                this._updateInputHint();
            }
        }));
        this._widgetDisposableStore.add(this._codeEditorWidget.onDidChangeModelContent(() => {
            if (this.isVisible()) {
                this._updateInputHint();
            }
        }));
        this._widgetDisposableStore.add(this._codeEditorWidget.onDidChangeModelDecorations(() => {
            if (this.isVisible()) {
                this._updateInputHint();
            }
        }));
        const cursorAtBoundaryContext = INTERACTIVE_INPUT_CURSOR_BOUNDARY.bindTo(this._contextKeyService);
        if (input.resource && ( input.historyService.has(input.resource))) {
            cursorAtBoundaryContext.set("top");
        } else {
            cursorAtBoundaryContext.set("none");
        }
        this._widgetDisposableStore.add(this._codeEditorWidget.onDidChangeCursorPosition((
            {
                position
            }
        ) => {
            const viewModel = this._codeEditorWidget._getViewModel();
            const lastLineNumber = viewModel.getLineCount();
            const lastLineCol = viewModel.getLineLength(lastLineNumber) + 1;
            const viewPosition = viewModel.coordinatesConverter.convertModelPositionToViewPosition(position);
            const firstLine = viewPosition.lineNumber === 1 && viewPosition.column === 1;
            const lastLine = viewPosition.lineNumber === lastLineNumber && viewPosition.column === lastLineCol;
            if (firstLine) {
                if (lastLine) {
                    cursorAtBoundaryContext.set("both");
                } else {
                    cursorAtBoundaryContext.set("top");
                }
            } else {
                if (lastLine) {
                    cursorAtBoundaryContext.set("bottom");
                } else {
                    cursorAtBoundaryContext.set("none");
                }
            }
        }));
        this._widgetDisposableStore.add(editorModel.onDidChangeContent(() => {
            const value = editorModel.getValue();
            if (this.input?.resource && value !== "") {
                const historyService = this.input.historyService;
                if (!historyService.matchesCurrent(this.input.resource, value)) {
                    historyService.replaceLast(this.input.resource, value);
                }
            }
        }));
        this._widgetDisposableStore.add(
            this._notebookWidget.value.onDidScroll(() => this._onDidChangeScroll.fire())
        );
        this._widgetDisposableStore.add(
            this._notebookWidget.value.onDidChangeViewCells(this.handleViewCellChange, this)
        );
        this._updateInputHint();
        this._syncWithKernel();
    }
    handleViewCellChange(e) {
        const notebookWidget = this._notebookWidget.value;
        if (!notebookWidget) {
            return;
        }
        for (const splice of e.splices) {
            const [_start, _delete, addedCells] = splice;
            if (addedCells.length) {
                const viewModel = notebookWidget.viewModel;
                if (viewModel) {
                    this.handleAppend(notebookWidget, viewModel);
                    break;
                }
            }
        }
    }
    handleAppend(notebookWidget, viewModel) {
        this._notebookWidgetService.updateReplContextKey(( viewModel.notebookDocument.uri.toString()));
        const navigateToCell = this._configurationService.getValue("accessibility.replEditor.autoFocusReplExecution");
        if (this._accessibilityService.isScreenReaderOptimized()) {
            if (navigateToCell === "lastExecution") {
                setTimeout(() => {
                    const lastCellIndex = viewModel.length - 1;
                    if (lastCellIndex >= 0) {
                        const cell = viewModel.viewCells[lastCellIndex];
                        notebookWidget.focusNotebookCell(cell, "container");
                    }
                }, 0);
            } else if (navigateToCell === "input") {
                this._codeEditorWidget.focus();
            }
        }
    }
    setOptions(options) {
        this._notebookWidget.value?.setOptions(options);
        super.setOptions(options);
    }
    _toEditorPaneSelectionChangeReason(e) {
        switch (e.source) {
        case TextEditorSelectionSource.PROGRAMMATIC:
            return EditorPaneSelectionChangeReason.PROGRAMMATIC;
        case TextEditorSelectionSource.NAVIGATION:
            return EditorPaneSelectionChangeReason.NAVIGATION;
        case TextEditorSelectionSource.JUMP:
            return EditorPaneSelectionChangeReason.JUMP;
        default:
            return EditorPaneSelectionChangeReason.USER;
        }
    }
    _cellAtBottom(cell) {
        const visibleRanges = this._notebookWidget.value?.visibleRanges || [];
        const cellIndex = this._notebookWidget.value?.getCellIndex(cell);
        if (cellIndex === Math.max(...( visibleRanges.map(range => range.end - 1)))) {
            return true;
        }
        return false;
    }
    _scrollIfNecessary(cvm) {
        const index = this._notebookWidget.value.getCellIndex(cvm);
        if (index === this._notebookWidget.value.getLength() - 1) {
            if (this._configurationService.getValue(ReplEditorSettings.interactiveWindowAlwaysScrollOnNewCell) || this._cellAtBottom(cvm)) {
                this._notebookWidget.value.scrollToBottom();
            }
        }
    }
    _syncWithKernel() {
        const notebook = this._notebookWidget.value?.textModel;
        const textModel = this._codeEditorWidget.getModel();
        if (notebook && textModel) {
            const info = this._notebookKernelService.getMatchingKernel(notebook);
            const selectedOrSuggested = info.selected ?? (info.suggestions.length === 1 ? info.suggestions[0] : undefined) ?? (info.all.length === 1 ? info.all[0] : undefined);
            if (selectedOrSuggested) {
                const language = selectedOrSuggested.supportedLanguages[0];
                if (language && language !== "plaintext") {
                    const newMode = this._languageService.createById(language).languageId;
                    textModel.setLanguage(newMode);
                }
                NOTEBOOK_KERNEL.bindTo(this._contextKeyService).set(selectedOrSuggested.id);
            }
        }
    }
    layout(dimension, position) {
        this._rootElement.classList.toggle("mid-width", dimension.width < 1000 && dimension.width >= 600);
        this._rootElement.classList.toggle("narrow-width", dimension.width < 600);
        const editorHeightChanged = dimension.height !== this._lastLayoutDimensions?.dimension.height;
        this._lastLayoutDimensions = {
            dimension,
            position
        };
        if (!this._notebookWidget.value) {
            return;
        }
        if (editorHeightChanged && this._codeEditorWidget) {
            SuggestController.get(this._codeEditorWidget)?.cancelSuggestWidget();
        }
        this._notebookEditorContainer.style.height = `${this._lastLayoutDimensions.dimension.height - this.inputCellContainerHeight}px`;
        this._layoutWidgets(dimension, position);
    }
    _layoutWidgets(dimension, position) {
        const contentHeight = this._codeEditorWidget.hasModel() ? this._codeEditorWidget.getContentHeight() : this.inputCellEditorHeight;
        const maxHeight = Math.min(dimension.height / 2, contentHeight);
        const leftMargin = this._notebookOptions.getCellEditorContainerLeftMargin();
        const inputCellContainerHeight = maxHeight + INPUT_CELL_VERTICAL_PADDING * 2;
        this._notebookEditorContainer.style.height = `${dimension.height - inputCellContainerHeight}px`;
        this._notebookWidget.value.layout(
            dimension.with(dimension.width, dimension.height - inputCellContainerHeight),
            this._notebookEditorContainer,
            position
        );
        this._codeEditorWidget.layout(this._validateDimension(
            dimension.width - leftMargin - INPUT_CELL_HORIZONTAL_PADDING_RIGHT,
            maxHeight
        ));
        this._inputFocusIndicator.style.height = `${contentHeight}px`;
        this._inputCellContainer.style.top = `${dimension.height - inputCellContainerHeight}px`;
        this._inputCellContainer.style.width = `${dimension.width}px`;
    }
    _validateDimension(width, height) {
        return new Dimension(Math.max(0, width), Math.max(0, height));
    }
    _hasConflictingDecoration() {
        return Boolean(this._codeEditorWidget.getLineDecorations(1)?.find(
            d => d.options.beforeContentClassName || d.options.afterContentClassName || d.options.before?.content || d.options.after?.content
        ));
    }
    _updateInputHint() {
        if (!this._codeEditorWidget) {
            return;
        }
        const shouldHide = !this._codeEditorWidget.hasModel() || this._configurationService.getValue(ReplEditorSettings.showExecutionHint) === false || this._codeEditorWidget.getModel().getValueLength() !== 0 || this._hasConflictingDecoration();
        if (!this._hintElement && !shouldHide) {
            this._hintElement = this._instantiationService.createInstance(ReplInputHintContentWidget, this._codeEditorWidget);
        } else if (this._hintElement && shouldHide) {
            this._hintElement.dispose();
            this._hintElement = undefined;
        }
    }
    getScrollPosition() {
        return {
            scrollTop: this._notebookWidget.value?.scrollTop ?? 0,
            scrollLeft: 0
        };
    }
    setScrollPosition(position) {
        this._notebookWidget.value?.setScrollTop(position.scrollTop);
    }
    focus() {
        super.focus();
        this._notebookWidget.value?.onShow();
        this._codeEditorWidget.focus();
    }
    focusHistory() {
        this._notebookWidget.value.focus();
    }
    setEditorVisible(visible) {
        super.setEditorVisible(visible);
        this._groupListener.value = this.group.onWillCloseEditor(e => this._saveEditorViewState(e.editor));
        if (!visible) {
            this._saveEditorViewState(this.input);
            if (this.input && this._notebookWidget.value) {
                this._notebookWidget.value.onWillHide();
            }
        }
        this._updateInputHint();
    }
    clearInput() {
        if (this._notebookWidget.value) {
            this._saveEditorViewState(this.input);
            this._notebookWidget.value.onWillHide();
        }
        this._codeEditorWidget?.dispose();
        this._notebookWidget = {
            value: undefined
        };
        this._widgetDisposableStore.clear();
        super.clearInput();
    }
    getControl() {
        return {
            notebookEditor: this._notebookWidget.value,
            activeCodeEditor: this.getActiveCodeEditor(),
            onDidChangeActiveEditor: Event.None
        };
    }
    getActiveCodeEditor() {
        if (!this._codeEditorWidget) {
            return undefined;
        }
        return this._codeEditorWidget.hasWidgetFocus() || !this._notebookWidget.value?.activeCodeEditor ? this._codeEditorWidget : this._notebookWidget.value.activeCodeEditor;
    }
};
ReplEditor = ( __decorate([( __param(1, ITelemetryService)), ( __param(2, IThemeService)), ( __param(3, IStorageService)), ( __param(4, IInstantiationService)), ( __param(5, INotebookEditorService)), ( __param(6, IContextKeyService)), ( __param(7, INotebookKernelService)), ( __param(8, ILanguageService)), ( __param(9, IKeybindingService)), ( __param(10, IConfigurationService)), ( __param(11, IMenuService)), ( __param(12, IContextMenuService)), ( __param(13, IEditorGroupsService)), ( __param(14, ITextResourceConfigurationService)), ( __param(15, INotebookExecutionStateService)), ( __param(16, IExtensionService)), ( __param(17, IAccessibilityService))], ReplEditor));
function isReplEditorControl(control) {
    const candidate = control;
    return candidate?.activeCodeEditor instanceof CodeEditorWidget && candidate?.notebookEditor instanceof NotebookEditorWidget;
}

export { ReplEditor, isReplEditorControl };
