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

part 'outlet_loader_event.dart';
part 'outlet_loader_state.dart';
part 'outlet_loader_bloc.freezed.dart';

class OutletLoaderBloc extends Bloc<OutletLoaderEvent, OutletLoaderState> {
  final OutletRemoteDataSource _outletRemoteDataSource;
  OutletLoaderBloc(this._outletRemoteDataSource)
      : super(OutletLoaderState.initial()) {
    on<_GetOutlet>((event, emit) async {
      emit(const _Loading());
      final result = await _outletRemoteDataSource.getOutlets();
      result.fold(
        (l) => emit(_Error(l)),
        (r) => emit(_Loaded(r.data?.outlets ?? [])),
      );
    });
  }
}
