export function getUserPref(userId, key, defaultValue) {
    if (!userId) return defaultValue;
    try {
        const raw = localStorage.getItem(`userPrefs_${userId}`);
        if (!raw) return defaultValue;
        const prefs = JSON.parse(raw);
        return prefs[key] !== undefined ? prefs[key] : defaultValue;
    } catch {
        return defaultValue;
    }
}

export function setUserPref(userId, key, value, settings) {
    if (!userId) return;
    try {
        const raw = localStorage.getItem(`userPrefs_${userId}`);
        const prefs = raw ? JSON.parse(raw) : {};
        prefs[key] = value;
        localStorage.setItem(`userPrefs_${userId}`, JSON.stringify(prefs));
        // Also persist to disk via platform settings (fire-and-forget)
        if (settings) {
            settings.set(`userPrefs_${userId}`, prefs);
        }
    } catch {
        // Silently fail on corrupt data or full storage
    }
}
