const apiBaseUrl = import.meta.env?.VITE_API_BASE_URL ?? '/api/v1'; export async function searchCatalog({ query = '', category = '', minPriceMinor, maxPriceMinor, sort = 'recent', page = 1, limit = 20 } = {}) { const params = new URLSearchParams({ query, page: String(page), limit: String(limit) }); if (sort !== 'recent') params.set('sort', sort); if (category) params.set('category', category); if (typeof minPriceMinor === 'number') params.set('minPriceMinor', String(minPriceMinor)); if (typeof maxPriceMinor === 'number') params.set('maxPriceMinor', String(maxPriceMinor)); const response = await fetch(`${apiBaseUrl}/public/search?${params}`); if (!response.ok) { throw new Error('Catalog search is unavailable.'); } const payload = await response.json(); return payload.data; } export async function listPublicCategories() { const response = await fetch(`${apiBaseUrl}/public/lookups/categories`); if (!response.ok) throw new Error('Categories are unavailable.'); return (await response.json()).data; } export async function getPublicListing(id) { const response = await fetch(`${apiBaseUrl}/public/listings/${encodeURIComponent(id)}`); if (!response.ok) { throw new Error(response.status === 404 ? 'Listing not found.' : 'Listing is unavailable.'); } const payload = await response.json(); return payload.data; } export async function getPublicProduct(slug) { const response = await fetch(`${apiBaseUrl}/public/products/${encodeURIComponent(slug)}`); if (!response.ok) { throw new Error(response.status === 404 ? 'Product not found.' : 'Product is unavailable.'); } const payload = await response.json(); return payload.data; }