import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/outlet_remote_data_source.dart';
import 'package:enaklo_pos/presentation/home/models/outlet_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'current_outlet_event.dart';
part 'current_outlet_state.dart';
part 'current_outlet_bloc.freezed.dart';

class CurrentOutletBloc extends Bloc<CurrentOutletEvent, CurrentOutletState> {
  final OutletRemoteDataSource _outletRemoteDataSource;
  CurrentOutletBloc(this._outletRemoteDataSource)
      : super(CurrentOutletState.initial()) {
    on<_CurrentOutlet>((event, emit) async {
      emit(const _Loading());
      final result = await _outletRemoteDataSource.currentOutlet();
      result.fold(
        (l) => emit(_Error(l)),
        (r) => emit(_Loaded(r.data!)),
      );
    });
  }
}
