import 'package:bloc/bloc.dart';
import 'package:tanami_app/features/countrySelection/bloc/GetCountry/get_country_event.dart';

import '../../../../Api_Helper/base_manager.dart';
import '../../../../Globalconst.dart';
import '../../domain/model/get_country_model.dart';
import '../../repositories/get_country_api.dart';
import 'get_country_state.dart';

class GetCountryBlock extends Bloc<GetCountryEvent, GetCountryState> {
  GetCountryBlock() : super(CountryInitial()) {
    on<GetCountry>(mapEventToState);
  }
  Future<void> mapEventToState(
      GetCountry event, Emitter<GetCountryState> emit) async {
    emit(CountryLoading());
    try {
      ResponseData response = await GetCountryAPI().getcountryAPI();
      if (response.status == ResponseStatus.SUCCESS) {
        GetCountryModel countryModel = GetCountryModel.fromJson(response.data);
        emit(CountryLoaded(countryModel));
      }
    } catch (e) {
      emit(CountryError(Globalconst.languageSelected == "en"
          ? 'Oops Something went wrong'
          : "تبا شيء ما حدث بشكل خاطئ"));
    }
  }
}
