import { joinLibraryTagNames, type PlacesLibraryDetailResponse, type PlacesLibraryKind, type PlacesLibraryPageResponse, type PlacesLibraryQueryInput, type PlacesLibraryRateResponse, } from '@trek/shared'; import apiClient from './client'; const base = '/addons/collections/library/places'; /** * The query as the server reads it: a tag filter (PLC-21) as its names joined by commas, a comma * inside a name escaped (`joinLibraryTagNames`), not `tags[]=`. */ function queryParams(query: PlacesLibraryQueryInput): Record { const { tags, ...rest } = query; if (tags === undefined) return rest; const joined = Array.isArray(tags) ? joinLibraryTagNames(tags) : tags; return joined === '' ? rest : { ...rest, tags: joined }; } /** PLC-07/PLC-08 "All places": one page of entries, and one entry by the trip place or saved place it was opened from. */ export const placesLibraryApi = { list: (query: PlacesLibraryQueryInput = {}, signal?: AbortSignal): Promise => apiClient.get(base, { params: queryParams(query), signal }).then((r) => r.data), detail: (kind: PlacesLibraryKind, id: number, signal?: AbortSignal): Promise => apiClient.get(`${base}/${kind}/${id}`, { signal }).then((r) => r.data), /** The reader's vote on an entry, on every copy of it they may vote on; null takes it back. */ rate: (key: string, rating: number | null): Promise => rating === null ? apiClient.delete(`${base}/rating`, { params: { key } }).then((r) => r.data) : apiClient.put(`${base}/rating`, { key, rating }).then((r) => r.data), };