import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../core/api/api_providers.dart';
import '../../../core/api/guarded_api_call.dart';
import '../../auth/data/auth_providers.dart';
import '../domain/shopping_list_item.dart';
import 'shopping_list_repository.dart';

final shoppingListRepositoryProvider = Provider<ShoppingListRepository>((ref) {
  return ShoppingListRepository(ref.watch(apiClientProvider));
});

final shoppingListItemsProvider = FutureProvider<List<ShoppingListItem>>((ref) async {
  final token = await ref.watch(authStateProvider.future);
  return guardedApiCall(ref, () {
    return ref.read(shoppingListRepositoryProvider).fetchOpenItems(token: token);
  });
});
