import { useMemo, useState } from 'react';
import { useParams } from 'react-router';

import { ListContext } from '/@/renderer/context/list-context';
import { AlbumListContent } from '/@/renderer/features/albums/components/album-list-content';
import { AlbumListHeader } from '/@/renderer/features/albums/components/album-list-header';
import { AnimatedPage } from '/@/renderer/features/shared/components/animated-page';
import { PageErrorBoundary } from '/@/renderer/features/shared/components/page-error-boundary';
import { AlbumListQuery } from '/@/shared/types/domain-types';
import { ItemListKey } from '/@/shared/types/types';

const getPageKey = (options: { albumArtistId?: string; genreId?: string }) => {
    if (options.albumArtistId) {
        return ItemListKey.ALBUM_ARTIST_ALBUM;
    }

    if (options.genreId) {
        return ItemListKey.GENRE_ALBUM;
    }

    return ItemListKey.ALBUM;
};

const AlbumListRoute = () => {
    const { albumArtistId, genreId } = useParams();
    const pageKey = getPageKey({ albumArtistId, genreId });

    const [itemCount, setItemCount] = useState<number | undefined>(undefined);

    const customFilters: Partial<AlbumListQuery> = useMemo(() => {
        return {
            artistIds: albumArtistId ? [albumArtistId] : undefined,
            genreIds: genreId ? [genreId] : undefined,
        };
    }, [albumArtistId, genreId]);

    const providerValue = useMemo(() => {
        return {
            customFilters,
            id: albumArtistId ?? genreId,
            itemCount,
            pageKey,
            setItemCount,
        };
    }, [albumArtistId, customFilters, genreId, itemCount, pageKey]);

    return (
        <AnimatedPage>
            <ListContext.Provider value={providerValue}>
                <AlbumListHeader />
                <AlbumListContent />
            </ListContext.Provider>
        </AnimatedPage>
    );
};

const AlbumListRouteWithBoundary = () => {
    return (
        <PageErrorBoundary>
            <AlbumListRoute />
        </PageErrorBoundary>
    );
};

export default AlbumListRouteWithBoundary;
