import { createInfiniteQuery } from '@tanstack/svelte-query';
import { api } from './client';
import { qk } from './queries';
import { pageGetNextPageParam } from './paging';
import type { AlbumRef, AlbumDetail, Page, TrackRef } from './types';

export const ALBUM_PAGE_SIZE = 50;

export async function listAlbumsAlpha(limit: number, offset: number): Promise<Page<AlbumRef>> {
  return api.get<Page<AlbumRef>>(`/api/library/albums?limit=${limit}&offset=${offset}`);
}

export function createAlbumsAlphaInfiniteQuery() {
  return createInfiniteQuery({
    queryKey: qk.albumsAlpha(),
    queryFn: ({ pageParam = 0 }) => listAlbumsAlpha(ALBUM_PAGE_SIZE, pageParam),
    initialPageParam: 0,
    getNextPageParam: pageGetNextPageParam<AlbumRef>()
  });
}

export async function listAlbumTracks(albumId: string): Promise<TrackRef[]> {
  const detail = await api.get<AlbumDetail>(`/api/albums/${albumId}`);
  return detail.tracks;
}
