
import '../../../../../food_delivery_client.dart';

part 'language_event.dart';

part 'language_state.dart';

part 'language_bloc.freezed.dart';
@injectable
class LanguageBloc extends Bloc<LanguageEvent, LanguageState> {
  final StorageService _storageService;

  LanguageBloc(this._storageService) : super(const LanguageState()) {
    on<_Started>(_onStarted);

    on<_Changeded>(_onChanged);
  }

  Future<void> _onStarted(_Started event, Emitter<LanguageState> emit) async {
    final lang = _storageService.getString(key: AppLocaleKeys.language);

    if (lang != null) {
      emit(state.copyWith(currentLocale: Locale(lang)));
    } else {
      emit(state.copyWith(currentLocale: Locale("en")));
    }
  }

  Future<void> _onChanged(_Changeded event, Emitter<LanguageState> emit) async {
    _storageService.setString(
      key: AppLocaleKeys.language,
      value: event.locale.languageCode,
    );
    emit(state.copyWith(currentLocale: event.locale));
  }
}
