import 'package:cargocalculaterapp/features/home/data/models/order_single_response.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../domain/usecase/order_single_usecase.dart';

part 'order_single_event.dart';

part 'order_single_state.dart';

class OrderSingleBloc extends Bloc<OrderSingleEvent, OrderSingleState> {
  OrderSingleBloc(this.orderSingleUseCase)
    : super(const OrderSingleState(isLoading: false)) {
    on<GetOrderSingleEvent>(_getOrderInfo);
  }

  final OrderSingleUseCase orderSingleUseCase;

  Future<void> _getOrderInfo(
    GetOrderSingleEvent event,
    Emitter<OrderSingleState> emit,
  ) async {
    emit(state.copyWith(isLoading: true));
    final response = await orderSingleUseCase(event.orderId);
    response.fold(
      (l) {
        emit(state.copyWith(isLoading: false));
      },
      (r) {
        emit(state.copyWith(isLoading: false, orderSingleResponse: r));
      },
    );
  }
}
