import type { PlacesLibraryDetailResponse, PlacesLibraryKind, PlacesLibraryPageResponse, PlacesLibraryQueryInput, } 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 ids joined by commas, not `tag_ids[]=`. */ function queryParams(query: PlacesLibraryQueryInput): Record { const { tag_ids: tagIds, ...rest } = query; if (tagIds === undefined) return rest; const joined = Array.isArray(tagIds) ? tagIds.join(',') : tagIds; return joined === '' ? rest : { ...rest, tag_ids: 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), };