import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../domain/bracket.dart';
import '../infrastructure/brackets_repository.dart';

part 'brackets_notifier.g.dart';

/// Currently-selected bracket id used when navigating to the detail screen.
@riverpod
class SelectedBracketId extends _$SelectedBracketId {
  @override
  String? build() => null;

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

/// Resolves a single [Bracket] by id out of the brackets stream. Returns null
/// while loading or if no bracket matches.
@riverpod
Bracket? bracketById(BracketByIdRef ref, String id) {
  final brackets = ref.watch(bracketsStreamProvider).valueOrNull;
  if (brackets == null) return null;
  for (final bracket in brackets) {
    if (bracket.id == id) return bracket;
  }
  return null;
}
