namespace YMhut.Box.Core.Settings;

internal static class PinnedToolSettings
{
    public const int MaxPinnedTools = 64;

    public static void Toggle(IList<string> pinnedToolIds, string toolId, bool wasPinned)
    {
        if (string.IsNullOrWhiteSpace(toolId))
        {
            return;
        }

        for (var index = pinnedToolIds.Count - 1; index >= 0; index--)
        {
            if (string.Equals(pinnedToolIds[index], toolId, StringComparison.OrdinalIgnoreCase))
            {
                pinnedToolIds.RemoveAt(index);
            }
        }

        if (!wasPinned)
        {
            pinnedToolIds.Insert(0, toolId);
        }

        Trim(pinnedToolIds);
    }

    public static void Trim(IList<string> pinnedToolIds)
    {
        if (pinnedToolIds.Count <= MaxPinnedTools)
        {
            return;
        }

        for (var index = pinnedToolIds.Count - 1; index >= MaxPinnedTools; index--)
        {
            pinnedToolIds.RemoveAt(index);
        }
    }
}
