private static IList <SpotifyAlbum> ProcessReleases(IList <SpotifyAlbum> input, PlaylistCreationOptions opts)
    {
        var releases = new List <SpotifyAlbum>();

        if (opts.IncludeAlbums)
        {
            releases.AddRange(input.Where(r => r.ReleaseType == ReleaseType.Album));
        }
        if (opts.IncludeCompilations)
        {
            releases.AddRange(input.Where(r => r.ReleaseType == ReleaseType.Compilation));
        }
        if (opts.IncludeSingles)
        {
            releases.AddRange(input.Where(r => r.ReleaseType == ReleaseType.Single));
        }

        return(opts.SortBy switch
        {
            SortMode.ByArtist => releases.OrderBy(r => String.Join(", ", r.Artists)).ToList(),
            SortMode.ByReleaseName => releases.OrderBy(r => r.ReleaseName).ToList(),
            SortMode.ByReleaseDate => releases.OrderBy(r => r.ReleaseDate).ToList(),
            SortMode.Random => releases.OrderBy(_ => Guid.NewGuid()).ToList(),
            var _ => releases
        });
        public async Task <Playlist> CreatePlaylist(PlaylistCreationOptions options)
        {
            var name = options.Name;

            var folderName = _fileSystem.GetValidFilename(name) + " [playlist]";

            var parentFolder = GetPlaylistsFolder(null);

            if (parentFolder == null)
            {
                throw new ArgumentException();
            }

            if (string.IsNullOrWhiteSpace(options.MediaType))
            {
                foreach (var itemId in options.ItemIdList)
                {
                    var item = _libraryManager.GetItemById(itemId);

                    if (item == null)
                    {
                        throw new ArgumentException("No item exists with the supplied Id");
                    }

                    if (!string.IsNullOrWhiteSpace(item.MediaType))
                    {
                        options.MediaType = item.MediaType;
                    }
                    else if (item is MusicArtist || item is MusicAlbum || item is MusicGenre)
                    {
                        options.MediaType = MediaType.Audio;
                    }
                    else if (item is Genre)
                    {
                        options.MediaType = MediaType.Video;
                    }
                    else
                    {
                        var folder = item as Folder;
                        if (folder != null)
                        {
                            options.MediaType = folder.GetRecursiveChildren()
                                                .Where(i => !i.IsFolder)
                                                .Select(i => i.MediaType)
                                                .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(options.MediaType))
                    {
                        break;
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(options.MediaType))
            {
                throw new ArgumentException("A playlist media type is required.");
            }

            var path = Path.Combine(parentFolder.Path, folderName);

            path = GetTargetPath(path);

            _iLibraryMonitor.ReportFileSystemChangeBeginning(path);

            try
            {
                Directory.CreateDirectory(path);

                var playlist = new Playlist
                {
                    Name        = name,
                    Parent      = parentFolder,
                    Path        = path,
                    OwnerUserId = options.UserId
                };

                playlist.SetMediaType(options.MediaType);

                await parentFolder.AddChild(playlist, CancellationToken.None).ConfigureAwait(false);

                await playlist.RefreshMetadata(new MetadataRefreshOptions { ForceSave = true }, CancellationToken.None)
                .ConfigureAwait(false);

                if (options.ItemIdList.Count > 0)
                {
                    await AddToPlaylist(playlist.Id.ToString("N"), options.ItemIdList);
                }

                return(playlist);
            }
            finally
            {
                // Refresh handled internally
                _iLibraryMonitor.ReportFileSystemChangeComplete(path, false);
            }
        }