import { useCallback } from 'react';

import { useSearchTermFilter } from '/@/renderer/features/shared/hooks/use-search-term-filter';
import { useSortByFilter } from '/@/renderer/features/shared/hooks/use-sort-by-filter';
import { useSortOrderFilter } from '/@/renderer/features/shared/hooks/use-sort-order-filter';
import { FILTER_KEYS } from '/@/renderer/features/shared/utils';
import { AlbumArtistListSort, SortOrder } from '/@/shared/types/domain-types';
import { ItemListKey } from '/@/shared/types/types';

export const useAlbumArtistListFilters = () => {
    const { setSortBy, sortBy } = useSortByFilter<AlbumArtistListSort>(
        null,
        ItemListKey.ALBUM_ARTIST,
    );

    const { setSortOrder, sortOrder } = useSortOrderFilter(null, ItemListKey.ALBUM_ARTIST);

    const { searchTerm, setSearchTerm } = useSearchTermFilter('');

    const clear = useCallback(() => {
        setSearchTerm(null);
        setSortBy(AlbumArtistListSort.NAME);
        setSortOrder(SortOrder.ASC);
    }, [setSearchTerm, setSortBy, setSortOrder]);

    const query = {
        [FILTER_KEYS.SHARED.SEARCH_TERM]: searchTerm ?? undefined,
        [FILTER_KEYS.SHARED.SORT_BY]: sortBy ?? undefined,
        [FILTER_KEYS.SHARED.SORT_ORDER]: sortOrder ?? undefined,
    };

    return {
        clear,
        query,
        setSearchTerm,
    };
};
