import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../domain/event.dart';
import '../infrastructure/events_repository.dart';

part 'events_notifier.g.dart';

/// Holds the currently-selected event id used when navigating to the detail
/// screen. Null when no event is selected.
@riverpod
class SelectedEventId extends _$SelectedEventId {
  @override
  String? build() => null;

  void select(String? id) => state = id;
}

/// Resolves a single [Event] by id out of the events stream. Returns null
/// while loading or if no event matches.
@riverpod
Event? eventById(EventByIdRef ref, String id) {
  final events = ref.watch(eventsStreamProvider).valueOrNull;
  if (events == null) return null;
  for (final event in events) {
    if (event.id == id) return event;
  }
  return null;
}
