import api from './axiosInstance.api'; // ============================================================================ // INTERFACES // ============================================================================ export interface EarthquakeAlert { warning_message: string; effective_start_time: string; depth: string; latitude: number; longitude: number; magnitude: string; disaster_type: string; direction: string; source: string; polygons?: Array<{ intensity: number; color: string; latitude: number; longitude: number; radius: number; name: string; coordinates: { type: string; coordinates: any; }; }>; } export interface NowcastAlert { severity: string; effective_start_time: string; effective_end_time: string; area_description: string; events: string; event_category: string; severity_color: string; location: { coordinates: [number, number]; // [longitude, latitude] in GeoJSON format type: string; }; source: string; identifier: string; state_id: string; entry_time: string; } export interface GeneralAlert { severity: string; identifier: number; effective_start_time: string; effective_end_time: string; disaster_type: string; area_description: string; warning_message: string; severity_color: string; severity_level: string; centroid: string; // "longitude,latitude" format alert_source: string; area_covered: string; sender_org_id: string; type: number; actual_lang: string; disseminated: string; alert_id_sdma_autoinc: number; } export interface EarthquakeAlertsResponse { alerts: EarthquakeAlert[]; } export interface NowcastAlertsResponse { nowcastDetails: NowcastAlert[]; } export interface AllAlertsResponse { earthquakes: EarthquakeAlertsResponse; nowcast: NowcastAlertsResponse; general: GeneralAlert[]; } export interface StatewiseFilter { event: string; Warn_type: string; action: string; view: string; noalert: string; sachethead: string; localert: string; issued_by: string; state_wise: string; loc_wise: string; search_state: string; search_loc: string; state: string; noalert_msg: string; } // ============================================================================ // API FUNCTIONS // ============================================================================ /** * Fetch all alerts (earthquakes, nowcast, and general) in a single optimized request */ export const getAllAlerts = async (): Promise => { const response = await api.get<{ success: boolean; data: AllAlertsResponse }>('/sachet/all-alerts'); return response.data.data; }; /** * Fetch earthquake alerts only */ export const getEarthquakeAlerts = async (): Promise => { const response = await api.get<{ success: boolean; data: EarthquakeAlertsResponse }>('/sachet/earthquake-alerts'); return response.data.data; }; /** * Fetch IMD nowcast weather alerts only */ export const getNowcastAlerts = async (): Promise => { const response = await api.get<{ success: boolean; data: NowcastAlertsResponse }>('/sachet/nowcast-alerts'); return response.data.data; }; /** * Fetch general alert details only */ export const getGeneralAlerts = async (): Promise => { const response = await api.get<{ success: boolean; data: GeneralAlert[] }>('/sachet/all-alert-details'); return response.data.data; }; /** * Fetch statewise filter configuration */ export const getStatewiseFilter = async (): Promise => { const response = await api.get<{ success: boolean; data: StatewiseFilter }>('/sachet/statewise-filter'); return response.data.data; }; // ============================================================================ // UTILITY FUNCTIONS // ============================================================================ /** * Convert GeoJSON coordinates [lng, lat] to Leaflet format [lat, lng] */ export const geoJsonToLeaflet = (coordinates: [number, number]): [number, number] => { return [coordinates[1], coordinates[0]]; }; /** * Parse centroid string "lng,lat" to Leaflet format [lat, lng] */ export const parseCentroid = (centroid: string): [number, number] | null => { const coords = centroid.split(',').map(Number); if (coords.length !== 2 || coords.some(isNaN)) { return null; } return [coords[1], coords[0]]; // [lat, lng] }; /** * Get severity color for alerts */ export const getSeverityColor = (severity: string): string => { const colors: { [key: string]: string } = { 'red': '#ef4444', 'orange': '#f97316', 'yellow': '#eab308', 'green': '#22c55e', 'EXTREME': '#dc2626', 'SEVERE': '#f97316', 'ALERT': '#f97316', 'WATCH': '#eab308', }; return colors[severity] || colors[severity?.toUpperCase()] || '#6b7280'; }; // ============================================================================ // DEFAULT EXPORT // ============================================================================ export default { getAllAlerts, getEarthquakeAlerts, getNowcastAlerts, getGeneralAlerts, getStatewiseFilter, geoJsonToLeaflet, parseCentroid, getSeverityColor, };