import { __rest } from "tslib";
import * as React from 'react';
import { useEffect, useState } from 'react';
import { ioc } from '@journeyapps-labs/reactor-mod';
import { MonacoStore } from '../stores/MonacoStore';
import { EnableVimSetting } from '../settings/VimSupportSetting';
import { observer } from 'mobx-react';
import * as vim from 'monaco-vim';
import { themed } from '../theme-reactor/editor-theme-fragment';
import { MonacoEditorWidget } from './MonacoEditorWidget';
var S;
(function (S) {
    S.Background = themed.div `
    display: flex;
    height: 100%;
    width: 100%;
    flex-direction: column;
    position: relative;
    overflow: hidden;
  `;
    S.Monaco = themed.div `
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

    // note: there is really no other way to do this
    // note: we have this code since we have the monaco editor background
    // set as transparent for a plethora of other reasons not listed here
    .sticky-widget,.monaco-quick-open-widget {
      background-color: ${(p) => p.theme.editor.backgroundSticky} !important;
    }
  `;
})(S || (S = {}));
export const EditorWidget = observer((props) => {
    var _a;
    const monacoStore = ioc.get(MonacoStore);
    const mergedSuggestOptions = Object.assign({ showInlineDetails: true }, (((_a = props.options) === null || _a === void 0 ? void 0 : _a.suggest) || {}));
    const _b = props.options || {}, { suggest: _ignoredSuggest } = _b, restOptions = __rest(_b, ["suggest"]);
    const ref = props.forwardRef || React.useRef(null);
    const [focusDisposer, setFocusDisposer] = useState(null);
    const e = React.useRef(null);
    useEffect(() => {
        return () => {
            if (e.current) {
                monacoStore.deregisterEditor(e.current);
            }
            focusDisposer === null || focusDisposer === void 0 ? void 0 : focusDisposer.dispose();
        };
    }, []);
    return (React.createElement(S.Background, { ref: ref },
        React.createElement(S.Monaco, null,
            React.createElement(MonacoEditorWidget, { model: props.model, options: Object.assign({ minimap: {
                        enabled: false
                    }, suggest: mergedSuggestOptions, automaticLayout: true, 
                    // https://github.com/microsoft/monaco-editor/issues/1993
                    // this was default true, which caused the model drift monitor to kick in and report errors (correctly)
                    trimAutoWhitespace: false, 
                    // autoIndent is required for XML files, not necessarily for others
                    autoIndent: 'full', fixedOverflowWidgets: true }, restOptions), editorDidMount: (editor) => {
                    e.current = editor;
                    // !------ react-monaco fixes their API (it broke after upgrade) -----
                    requestAnimationFrame(() => {
                        var _a, _b, _c;
                        /**
                         * There are certain conditions where patching the monaco shortcuts is not required.
                         * (patching shortcuts also introduces a slight overhead, which is why we allow this to be configurable)
                         * furthermore: some editors will always be readonly such as search previews and dont need these shortcuts
                         */
                        if (props.patchOxideShortcuts == null || props.patchOxideShortcuts === true) {
                            monacoStore.registerEditor(editor);
                        }
                        (_b = (_a = props.events) === null || _a === void 0 ? void 0 : _a.getEditor) === null || _b === void 0 ? void 0 : _b.call(_a, editor);
                        // !------------------------------------------------------------------
                        if ((_c = props.events) === null || _c === void 0 ? void 0 : _c.gotFocus) {
                            const focusDisposer = editor.onDidFocusEditorWidget(() => {
                                props.events.gotFocus(editor);
                            });
                            setFocusDisposer(focusDisposer);
                        }
                        if (EnableVimSetting.enabled()) {
                            vim.initVimMode(editor);
                        }
                    });
                } }))));
});
//# sourceMappingURL=EditorWidget.js.map