import AsyncStorage from '@react-native-async-storage/async-storage'; import type { Event, Station, ReminderSettings } from '@timetoleave/core'; // ── Keys ──────────────────────────────────────────── const EVENTS_KEY = '@timetoleave_events'; const ORIGIN_KEY = '@timetoleave_origin'; const NOTIFICATIONS_KEY = '@timetoleave_notifications'; // ── Default notification settings ───────────────────── const DEFAULT_NOTIFICATION_SETTINGS: ReminderSettings = { bufferMinutes: 30, enabled: true, }; // ── Helpers ─────────────────────────────────────────── function reviveDates(json: string): Event[] { try { const parsed = JSON.parse(json) as Array; return parsed.map((e) => ({ ...e, eventTime: new Date(e.eventTime) })); } catch { return []; } } // ── Events ──────────────────────────────────────────── export async function loadEvents(): Promise { const json = await AsyncStorage.getItem(EVENTS_KEY); return json ? reviveDates(json) : []; } export async function saveEvents(events: Event[]): Promise { const json = JSON.stringify(events); await AsyncStorage.setItem(EVENTS_KEY, json); } export async function addEvent(event: Event): Promise { const events = await loadEvents(); events.push(event); await saveEvents(events); } export async function removeEvent(id: string, onDone?: () => void): Promise { const events = await loadEvents(); const filtered = events.filter((e) => e.id !== id); await saveEvents(filtered); onDone?.(); } // ── Origin Station ──────────────────────────────────── export async function loadOriginStation(): Promise { const json = await AsyncStorage.getItem(ORIGIN_KEY); return json ? JSON.parse(json) : null; } export async function saveOriginStation(station: Station): Promise { const json = JSON.stringify(station); await AsyncStorage.setItem(ORIGIN_KEY, json); } // ── Notification Settings ───────────────────────────── export async function loadNotificationSettings(): Promise { const json = await AsyncStorage.getItem(NOTIFICATIONS_KEY); return json ? JSON.parse(json) : DEFAULT_NOTIFICATION_SETTINGS; } export async function saveNotificationSettings( settings: ReminderSettings, ): Promise { const json = JSON.stringify(settings); await AsyncStorage.setItem(NOTIFICATIONS_KEY, json); }