import 'package:enaklo_pos/data/datasources/table_remote_datasource.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:enaklo_pos/data/models/response/table_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'get_table_event.dart';
part 'get_table_state.dart';
part 'get_table_bloc.freezed.dart';

class GetTableBloc extends Bloc<GetTableEvent, GetTableState> {
  final TableRemoteDataSource _tableRemoteDataSource;
  GetTableBloc(this._tableRemoteDataSource) : super(GetTableState.initial()) {
    on<_GetTables>((event, emit) async {
      emit(_Loading());
      final tables = await _tableRemoteDataSource.getTable();
      tables.fold(
        (l) => emit(_Error(l)),
        (r) => emit(
          _Success(r.data!.tables!),
        ),
      );
    });
  }
}
