import { WiFiHotspot } from '../types'; const API_Base_URL = '/api/markers'; const isDebug = import.meta.env.DEV; export const apiService = { getHotspots: async (): Promise => { if (isDebug) console.log(`[API Call] GET ${window.location.origin}${API_Base_URL}`); try { const response = await fetch(API_Base_URL, { cache: 'no-store' }); if (!response.ok) throw new Error('Failed to fetch markers'); const data = await response.json(); if (isDebug) console.log(`[API Response] Received ${data.length} hotspots`, data); return data; } catch (e) { console.error("Failed to load hotspots from API", e); return []; } }, saveHotspot: async (hotspot: WiFiHotspot): Promise => { if (isDebug) console.log(`[API Call] POST ${API_Base_URL}`, hotspot); try { const response = await fetch(API_Base_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(hotspot), }); if (!response.ok) throw new Error('Failed to save marker'); const data = await response.json(); if (isDebug) console.log(`[API Response] Save success:`, data); // Trigger a storage event to refresh UI if needed (keeping compatibility) window.dispatchEvent(new Event('storage_updated')); } catch (e) { console.error("Failed to save hotspot", e); } }, deleteHotspot: async (id: string): Promise => { const url = `${API_Base_URL}/${id}`; if (isDebug) console.log(`[API Call] DELETE ${url}`); try { const response = await fetch(url, { method: 'DELETE', }); if (!response.ok) throw new Error('Failed to delete marker'); const data = await response.json(); if (isDebug) console.log(`[API Response] Delete success:`, data); window.dispatchEvent(new Event('storage_updated')); } catch (e) { console.error("Failed to delete hotspot", e); } } };