import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/customer_remote_datasource.dart';
import 'package:enaklo_pos/data/models/response/customer_response_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'customer_loader_event.dart';
part 'customer_loader_state.dart';
part 'customer_loader_bloc.freezed.dart';

class CustomerLoaderBloc
    extends Bloc<CustomerLoaderEvent, CustomerLoaderState> {
  final CustomerRemoteDataSource _customerRemoteDataSource;
  CustomerLoaderBloc(
    this._customerRemoteDataSource,
  ) : super(CustomerLoaderState.initial()) {
    on<_GetCustomer>((event, emit) async {
      emit(const _Loading());
      final result = await _customerRemoteDataSource.getCustomers();
      result.fold(
        (l) => emit(_Error(l)),
        (r) => emit(_Loaded(
          r.data?.customers ?? [],
          r.data?.totalCount ?? 0,
        )),
      );
    });
  }
}
