import { initVimMode as initVimMode$1, VimMode as keymap_vim_default } from '../node_modules/monaco-vim/dist/index.js';
import { configurationRegistry, ConfigurationScope } from '@codingame/monaco-vscode-configuration-service-override';
import { onConfigurationChanged, getConfiguration } from '../configuration/index.js';

const vimKeybindingsSchema = {
    type: 'object',
    properties: {
        before: {
            type: 'array',
            items: {
                type: 'string'
            }
        },
        after: {
            type: 'array',
            items: {
                type: 'string'
            }
        }
    }
};
configurationRegistry.registerConfiguration({
    id: 'vim',
    order: 5,
    type: 'object',
    title: 'Vim',
    scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,
    properties: {
        'vim.normalModeKeyBindings': {
            type: 'array',
            description: 'Remapped keys in Normal mode.',
            items: vimKeybindingsSchema
        },
        'vim.insertModeKeyBindings': {
            type: 'array',
            description: 'Remapped keys in Insert mode.',
            items: vimKeybindingsSchema
        },
        'vim.visualModeKeyBindings': {
            type: 'array',
            description: 'Remapped keys in Visual mode.',
            items: vimKeybindingsSchema
        }
    }
});
function mapVimKeyBindings(keybindingConfig, mode) {
    if (keybindingConfig != null) {
        for (const item of keybindingConfig) {
            if (item.after != null && item.before != null) {
                keymap_vim_default.Vim.map(item.before.join(''), item.after.join(''), mode);
            }
        }
    }
}
function unmapVimKeyBindings(keybindingConfig, mode) {
    if (keybindingConfig != null) {
        for (const item of keybindingConfig) {
            if (item.after != null && item.before != null) {
                keymap_vim_default.Vim.unmap(item.before.join(''), mode);
            }
        }
    }
}
let unmapPrevious = () => { };
onConfigurationChanged(() => {
    unmapPrevious();
    const config = getConfiguration(undefined, 'vim');
    mapVimKeyBindings(config?.insertModeKeyBindings, 'insert');
    mapVimKeyBindings(config?.normalModeKeyBindings, 'normal');
    mapVimKeyBindings(config?.visualModeKeyBindings, 'visual');
    unmapPrevious = () => {
        unmapVimKeyBindings(config?.insertModeKeyBindings, 'insert');
        unmapVimKeyBindings(config?.normalModeKeyBindings, 'normal');
        unmapVimKeyBindings(config?.visualModeKeyBindings, 'visual');
    };
});
function initVimMode(editor, statusBarElement) {
    return initVimMode$1(editor, statusBarElement);
}

export { initVimMode };
