window.monaco = monaco;

window.MonacoRuntime = {
    OnCodeEditorDidChangeModelContent: (codeEditor, dotnetRef, method) => {
        codeEditor.onDidChangeModelContent(function (e) {
            dotnetRef.invokeMethodAsync(method, e);
        });
    },
    CSharpLanguageRegisterCompletionItemProvider: (dotnetRef, getCompletionMethod) => {
        return monaco.languages.registerCompletionItemProvider("csharp", {
            triggerCharacters: ["."],
            //resolveCompletionItem: (model, position, item) => {
            //    return this.resolveCompletionItem(item, dotnetHelper)
            //},
            provideCompletionItems: async (model, position, context, token) => {
                return await dotnetRef.invokeMethodAsync(getCompletionMethod, position.lineNumber, position.column, context.triggerKind, context.triggerCharacter);
            }
        });
    },
    CSharpLanguageRegisterSignatureHelpProvider: (dotnetRef, getSignatureHelpMethod) => {
        return monaco.languages.registerSignatureHelpProvider("csharp", {
            signatureHelpTriggerCharacters: ['('],
            provideSignatureHelp: async (model, position, token, context) => {
                let signature = await dotnetRef.invokeMethodAsync(getSignatureHelpMethod, position.lineNumber, position.column);
                if (!signature) {
                    return undefined;
                }
                return {
                    value: signature.value,
                    dispose: () => { },
                }
            }
        });
    },
    CSharpLanguageRegisterHoverProvider: (dotnetRef, getHoverProviderMethod) => {
        return monaco.languages.registerHoverProvider("csharp", {
            provideHover: async (model, position, token, context) => {
                var mess = await dotnetRef.invokeMethodAsync(getHoverProviderMethod, position.lineNumber, position.column);
                return {
                    contents: [
                        {
                            value: mess
                        }
                    ]
                }
            }
        });
    }
}

window.addEventListener("keydown", function (e) {
    if ((e.key === 's' || e.key == 'S') && e.ctrlKey) {
        e.preventDefault();
        e.stopPropagation();
    }
}, false);

window.addEventListener("contextmenu", function (e) {
    e.preventDefault();
    e.stopPropagation();
}, false);

window.SetRadioChecked = function (id) {
    document.getElementById(id).checked = true;
};

window.UncheckRadioAll = function (name) {
    const radios = document.querySelectorAll(`input[type="radio"][name="${name}"]`);
    radios.forEach(radio => radio.checked = false);
};

window.ScrollToBottom = (element) => {
    if (element) {
        element.scrollTop = element.scrollHeight;
    }
};
