@using LANCommander.Models;
@using LANCommander.Services;
@inject IJSRuntime JS

@foreach (var group in Snippets.Select(s => s.Group).Distinct())
{
    <div class="dropdown">
        <button class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
            @group
        </button>
        <ul class="dropdown-menu">
            @foreach (var snippet in Snippets.Where(s => s.Group == group))
            {
                <li><a class="dropdown-item" @onclick="() => InsertSnippet(snippet)">@snippet.Name</a></li>
            }
        </ul>
    </div>
}


@code {
    public IEnumerable<Snippet> Snippets { get; set; }

    protected override void OnInitialized()
    {
        Snippets = ScriptService.GetSnippets();
    }

    private async Task InsertSnippet(Snippet snippet) {
        await JS.InvokeVoidAsync("Editor.trigger", "keyboard", "type", new { text = snippet.Content });
    }
}