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

part 'customer_form_event.dart';
part 'customer_form_state.dart';
part 'customer_form_bloc.freezed.dart';

class CustomerFormBloc extends Bloc<CustomerFormEvent, CustomerFormState> {
  final CustomerRemoteDataSource _customerRemoteDataSource;
  CustomerFormBloc(this._customerRemoteDataSource)
      : super(CustomerFormState.initial()) {
    on<_Create>((event, emit) async {
      emit(const _Loading());
      final result = await _customerRemoteDataSource.createCustomer(
        name: event.name,
        address: event.address,
        phone: event.phoneNumber,
        email: event.email,
        isActive: event.isActive,
      );
      result.fold(
        (l) => emit(_Error(l)),
        (r) => emit(_Success()),
      );
    });
  }
}
