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

part 'create_table_event.dart';
part 'create_table_state.dart';
part 'create_table_bloc.freezed.dart';

class CreateTableBloc extends Bloc<CreateTableEvent, CreateTableState> {
  final TableRemoteDataSource _tableRemoteDataSource;
  CreateTableBloc(this._tableRemoteDataSource)
      : super(CreateTableState.initial()) {
    on<_CreateTable>((event, emit) async {
      emit(_Loading());
      final result = await _tableRemoteDataSource.createTable(
        tableName: event.tableName,
        capacity: event.capacity,
        location: event.location,
      );

      result.fold((l) => emit(_Error(l)),
          (r) => emit(_Success('Meja berhasil dibuat')));
    });
  }
}
