import 'package:bloc/bloc.dart';
import 'package:proxibuy/data/repositories/offer_repository.dart';
import 'package:proxibuy/presentation/offer/bloc/offer_event.dart';
import 'package:proxibuy/presentation/offer/bloc/offer_state.dart';


class OffersBloc extends Bloc<OffersEvent, OffersState> {
  final OfferRepository _offerRepository;

  OffersBloc({required OfferRepository offerRepository})
      : _offerRepository = offerRepository,
        super(OffersInitial()) {
    on<OffersFetchRequested>(_onFetchRequested);
  }

  Future<void> _onFetchRequested(
    OffersFetchRequested event,
    Emitter<OffersState> emit,
  ) async {
    emit(OffersLoadInProgress());
    try {
      final offers = await _offerRepository.fetchOffers(
        selectedCategories: event.selectedCategories,
      );
      emit(OffersLoadSuccess(offers));
    } catch (e) {
      emit(OffersLoadFailure(e.toString()));
    }
  }
}