// <auto-generated/>
#pragma warning disable 1591
#pragma warning disable 0414
#pragma warning disable 0649
#pragma warning disable 0169

namespace RichTextEditor_Test.Pages
{
    #line hidden
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
#nullable restore
#line 1 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using System.Net.Http;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Microsoft.AspNetCore.Authorization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Microsoft.AspNetCore.Components.Authorization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 4 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Microsoft.AspNetCore.Components.Forms;

#line default
#line hidden
#nullable disable
#nullable restore
#line 5 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Microsoft.AspNetCore.Components.Routing;

#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Microsoft.AspNetCore.Components.Web;

#line default
#line hidden
#nullable disable
#nullable restore
#line 7 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Microsoft.JSInterop;

#line default
#line hidden
#nullable disable
#nullable restore
#line 8 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using RichTextEditor_Test;

#line default
#line hidden
#nullable disable
#nullable restore
#line 9 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using RichTextEditor_Test.Shared;

#line default
#line hidden
#nullable disable
#nullable restore
#line 10 "C:\Users\an\Downloads\RichTextEditor_Test\_Imports.razor"
using Syncfusion.Blazor;

#line default
#line hidden
#nullable disable
#nullable restore
#line 1 "C:\Users\an\Downloads\RichTextEditor_Test\Pages\RencoreButtonContextMenu.razor"
using Syncfusion.Blazor.SplitButtons;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "C:\Users\an\Downloads\RichTextEditor_Test\Pages\RencoreButtonContextMenu.razor"
using Microsoft.AspNetCore.Components;

#line default
#line hidden
#nullable disable
    public partial class RencoreButtonContextMenu : Microsoft.AspNetCore.Components.ComponentBase, IDisposable
    {
        #pragma warning disable 1998
        protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
        {
        }
        #pragma warning restore 1998
#nullable restore
#line 32 "C:\Users\an\Downloads\RichTextEditor_Test\Pages\RencoreButtonContextMenu.razor"
       
    private IEnumerable<RencoreMenuItem> items;
    private SfDropDownButton dropDownButton;
    private string itemsSerialized = "";

    [Parameter]
    public string DataUpId { get; set; } = "EmptyDataUp";

    [Parameter]
    public string CustomContent { get; set; } = "...";

    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    [Parameter]
    public bool Primary { get; set; }

    [Parameter]
    public IEnumerable<RencoreMenuItem> Items
    {
        get
        {
            return items;
        }

        set
        {
            items = value;

            ReloadItems();
        }
    }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public EventCallback<RencoreMenuItemClickEvent> OnItemClick { get; set; }

    [Parameter]
    public EventCallback<bool> OnOpenContextMenu { get; set; }

    [Parameter]
    public object Context { get; set; }

    private List<DropDownMenuItem> DropDownMenuItems;
    [Inject]
    private NavigationManager Navigation { get; set; }

    protected override void OnInitialized()
    {
        ReloadItems();
    }

    private void ReloadItems()
    {
        // only update the items if an update is needed, otherwise there is a lack when user clicks the button and
        // the menu appears seconds later
        if (this.Items != null)
        {
            var newitemsSerialized = Newtonsoft.Json.JsonConvert.SerializeObject(this.Items);
            if (newitemsSerialized == this.itemsSerialized)
            {
                // no update needed
                return;
            }
            else
            {
                // update needed
                this.itemsSerialized = newitemsSerialized;

                this.DropDownMenuItems = Items.Where(x => !x.Disabled && !x.Hidden).Select(x => new DropDownMenuItem()
                {
                    Disabled = x.Disabled,
                    Text = x.Text,
                    Separator = x.Separator,
                    Id = x.Id
                }).ToList();
            }
        }
        else
        {
            if (this.itemsSerialized != null)
            {
                this.itemsSerialized = null;
            }
        }
    }

    private async Task OnMenuOpen()
    {
        if (OnOpenContextMenu.HasDelegate)
        {
            await OnOpenContextMenu.InvokeAsync(true);
        }

        ReloadItems();
    }

    private async Task DropSelect(MenuEventArgs args)
    {
        RencoreMenuItem rencoreMenuItem = Items.FirstOrDefault(x => x != null && !string.IsNullOrEmpty(x.Id) && x.Id.Equals(args.Item.Id, StringComparison.InvariantCultureIgnoreCase));

        if(rencoreMenuItem != null)
        {
            if (OnItemClick.HasDelegate)
            {
                await OnItemClick.InvokeAsync(new RencoreMenuItemClickEvent() { Data = rencoreMenuItem, ItemId = args.Item.Id, Context = Context });
            }

            if (rencoreMenuItem != null && !string.IsNullOrEmpty(rencoreMenuItem.Url))
            {
                if (rencoreMenuItem.OpenInNewTab)
                {
                    await JSRuntime.InvokeVoidAsync("openInNewTab", rencoreMenuItem.Url);
                }
                else
                {
                    Navigation.NavigateTo(rencoreMenuItem.Url);
                }
            }
        }
    }

    public void Dispose()
    {
        try
        {
            if (dropDownButton != null)
            {
                dropDownButton.Dispose();
            }
        }
        catch (ObjectDisposedException ex)
        {
            // ignore
        }
    }

#line default
#line hidden
#nullable disable
        [global::Microsoft.AspNetCore.Components.InjectAttribute] private IJSRuntime JSRuntime { get; set; }
    }
}
#pragma warning restore 1591
