import { __exports as emacs } from '../../../../_virtual/index3.js';
import require$$0 from '../../../../_virtual/_monaco-editor.js';
import { __require as requireLodash_throttle } from '../../../lodash.throttle/index.js';
import { __require as requireLodash_kebabcase } from '../../../lodash.kebabcase/index.js';
import { __require as requireCommands } from './commands.js';
import { __require as requireUtil } from './util.js';
import { __require as requireBasicInput } from './basicInput.js';
import { __require as requireState } from './state.js';

var hasRequiredEmacs;

function requireEmacs () {
	if (hasRequiredEmacs) return emacs;
	hasRequiredEmacs = 1;
	Object.defineProperty(emacs, "__esModule", { value: true });
	emacs.getConfiguration = emacs.EmacsExtension = void 0;
	var monaco = require$$0;
	var throttle = requireLodash_throttle();
	var kebabCase = requireLodash_kebabcase();
	var commands_1 = requireCommands();
	var util_1 = requireUtil();
	var basicInput_1 = requireBasicInput();
	var state_1 = requireState();
	var _a = monaco.editor, TextEditorCursorBlinkingStyle = _a.TextEditorCursorBlinkingStyle, TextEditorCursorStyle = _a.TextEditorCursorStyle;
	var EmacsExtension = (function () {
	    function EmacsExtension(editor) {
	        this._disposables = [];
	        this._inSelectionMode = false;
	        this._changeDisposable = null;
	        this._state = new state_1.State();
	        this._onDidMarkChange = new util_1.Emitter();
	        this.onDidMarkChange = this._onDidMarkChange.event;
	        this._onDidChangeKey = new util_1.Emitter();
	        this.onDidChangeKey = this._onDidChangeKey.event;
	        this._editor = editor;
	        var config = getConfiguration(editor);
	        this._intialCursorType = config.cursorStyle;
	        this._intialCursorBlinking = config.cursorBlinking;
	        this._basicInputWidget = new basicInput_1.BasicInputWidget();
	    }
	    EmacsExtension.prototype.start = function () {
	        if (this._disposables.length) {
	            return;
	        }
	        this.addListeners();
	        this._editor.updateOptions({
	            cursorStyle: kebabCase(TextEditorCursorStyle[TextEditorCursorStyle.Block]),
	            cursorBlinking: kebabCase(TextEditorCursorBlinkingStyle[TextEditorCursorBlinkingStyle.Blink]),
	        });
	        this._editor.addOverlayWidget(this._basicInputWidget);
	    };
	    Object.defineProperty(EmacsExtension.prototype, "state", {
	        get: function () {
	            return this._state;
	        },
	        enumerable: false,
	        configurable: true
	    });
	    EmacsExtension.prototype.getEditor = function () {
	        return this._editor;
	    };
	    EmacsExtension.prototype.addListeners = function () {
	        this._disposables.push(this._editor.onKeyDown(this.onKeyDown.bind(this)));
	        this._throttledScroll = throttle(this.onEditorScroll.bind(this), 500);
	        this._disposables.push(this._editor.onDidScrollChange(this._throttledScroll));
	    };
	    EmacsExtension.prototype.cancelKey = function (ev) {
	        ev.preventDefault();
	        ev.stopPropagation();
	    };
	    EmacsExtension.prototype.onKeyDown = function (ev) {
	        if (ev.browserEvent.defaultPrevented) {
	            return;
	        }
	        var key = (0, util_1.monacoToEmacsKey)(ev);
	        if (!key) {
	            return;
	        }
	        key = this._state.updateAndGetKey(key);
	        this._onDidChangeKey.fire(this._state.getReadableState());
	        if (key === state_1.EAT_UP_KEY) {
	            this._onDidChangeKey.fire(this._state.getReadableState());
	            this.cancelKey(ev);
	            return;
	        }
	        var command = commands_1.COMMANDS[key];
	        if (!command) {
	            this._onDidChangeKey.fire(this._state.getReadableState());
	            this._state.setLastCommandKey(key);
	            return;
	        }
	        this.cancelKey(ev);
	        var repeatedTrigger = this._state.isLastCommandKey(key);
	        (0, commands_1.executeCommand)(this, command, this._state.getInputBuffer(), repeatedTrigger);
	        this._state.setLastCommandKey(key);
	        this._state.updateStateOnExecution();
	    };
	    EmacsExtension.prototype.onEditorScroll = function () {
	        var height = this._editor.getLayoutInfo().height;
	        var layout = this._editor.getScrolledVisiblePosition(this._editor.getPosition());
	        if (layout.top >= 0 && layout.top <= height) {
	            return;
	        }
	        var ranges = this._editor.getVisibleRanges();
	        if (!ranges.length) {
	            return;
	        }
	        var newPos;
	        var sel = this._editor.getSelection();
	        if (layout.top < 0) {
	            newPos = new monaco.Position(ranges[0].getStartPosition().lineNumber, 1);
	        }
	        else if (layout.top > height) {
	            newPos = new monaco.Position(ranges[ranges.length - 1].getEndPosition().lineNumber, 1);
	        }
	        if (this._inSelectionMode) {
	            this._editor.setSelection(monaco.Selection.fromPositions(sel.getStartPosition(), newPos));
	        }
	        else {
	            this._editor.setPosition(newPos);
	        }
	    };
	    EmacsExtension.prototype.onContentChange = function () {
	        this.selectionMode = false;
	    };
	    EmacsExtension.prototype.clearState = function () {
	        this._state.updateStateOnExecution();
	    };
	    Object.defineProperty(EmacsExtension.prototype, "selectionMode", {
	        get: function () {
	            return this._inSelectionMode;
	        },
	        set: function (mode) {
	            if (mode === this._inSelectionMode) {
	                return;
	            }
	            this._inSelectionMode = mode;
	            if (mode) {
	                this._changeDisposable = this._editor.onDidChangeModelContent(this.onContentChange.bind(this));
	            }
	            else if (this._changeDisposable) {
	                this._changeDisposable.dispose();
	                this._changeDisposable = null;
	            }
	            this._onDidMarkChange.fire(mode);
	        },
	        enumerable: false,
	        configurable: true
	    });
	    EmacsExtension.prototype.getCursorAnchor = function () {
	        var sel = this._editor.getSelection();
	        var dir = sel.getDirection();
	        return dir === monaco.SelectionDirection.LTR ? sel.getEndPosition() : sel.getStartPosition();
	    };
	    EmacsExtension.prototype.getBasicInput = function (message) {
	        return this._basicInputWidget.getInput(message);
	    };
	    EmacsExtension.prototype.dispose = function () {
	        this._disposables.forEach(function (d) { return d.dispose(); });
	        this._disposables = undefined;
	        if (this._changeDisposable) {
	            this._changeDisposable.dispose();
	            this._changeDisposable = null;
	        }
	        this._editor.updateOptions({
	            cursorStyle: this._intialCursorType,
	            cursorBlinking: this._intialCursorBlinking,
	        });
	        this._editor.removeOverlayWidget(this._basicInputWidget);
	        this._basicInputWidget.dispose();
	        this._throttledScroll.cancel();
	        this._state = null;
	    };
	    return EmacsExtension;
	}());
	emacs.EmacsExtension = EmacsExtension;
	function getConfiguration(editor) {
	    var cursorStyle = editor.getOption(monaco.editor.EditorOption.cursorStyle);
	    var cursorBlinking = editor.getOption(monaco.editor.EditorOption.cursorBlinking);
	    return {
	        cursorStyle: kebabCase(TextEditorCursorStyle[cursorStyle]),
	        cursorBlinking: kebabCase(TextEditorCursorBlinkingStyle[cursorBlinking]),
	    };
	}
	emacs.getConfiguration = getConfiguration;
	return emacs;
}

export { requireEmacs as __require };
