import * as SecureStore from 'expo-secure-store'; import { api } from './api'; const CONSENT_KEY = 'consents'; export interface Consents { essential: boolean; analytics: boolean; preferences: boolean; health_data: boolean; } const DEFAULT_CONSENTS: Consents = { essential: true, analytics: false, preferences: false, health_data: false, }; export async function getLocalConsents(): Promise { try { const stored = await SecureStore.getItemAsync(CONSENT_KEY); if (stored) return { ...DEFAULT_CONSENTS, ...JSON.parse(stored), essential: true }; } catch {} return { ...DEFAULT_CONSENTS }; } export async function setLocalConsents(consents: Consents): Promise { try { await SecureStore.setItemAsync(CONSENT_KEY, JSON.stringify(consents)); } catch {} } export async function hasConsentChoice(): Promise { try { return (await SecureStore.getItemAsync(CONSENT_KEY)) !== null; } catch { return false; } } export async function fetchServerConsents(): Promise { try { const res = await api.get('/consents'); await setLocalConsents(res.data); return res.data; } catch { return null; } } export async function saveConsents(consents: Consents): Promise { const toSave = { ...consents, essential: true }; await setLocalConsents(toSave); try { const res = await api.put('/consents', { categories: { analytics: toSave.analytics, preferences: toSave.preferences, health_data: toSave.health_data }, }); await setLocalConsents(res.data); return res.data; } catch { return toSave; } } export async function hasConsent(category: keyof Consents): Promise { const consents = await getLocalConsents(); return consents[category] === true; }