import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow_core/core.dart';
import 'package:krow_domain/krow_domain.dart';
import '../../domain/usecases/get_order_types_usecase.dart';
import 'client_create_order_event.dart';
import 'client_create_order_state.dart';

/// BLoC for managing the list of available order types.
class ClientCreateOrderBloc
    extends Bloc<ClientCreateOrderEvent, ClientCreateOrderState>
    with BlocErrorHandler<ClientCreateOrderState> {
  ClientCreateOrderBloc(this._getOrderTypesUseCase)
    : super(const ClientCreateOrderInitial()) {
    on<ClientCreateOrderTypesRequested>(_onTypesRequested);
  }
  final GetOrderTypesUseCase _getOrderTypesUseCase;

  Future<void> _onTypesRequested(
    ClientCreateOrderTypesRequested event,
    Emitter<ClientCreateOrderState> emit,
  ) async {
    await handleError(
      emit: emit,
      action: () async {
        final List<OrderType> types = await _getOrderTypesUseCase();
        emit(ClientCreateOrderLoadSuccess(types));
      },
      onError: (String errorKey) => ClientCreateOrderLoadFailure(errorKey),
    );
  }
}

