import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/space_model.dart';

part 'communities_tree_selection_event.dart';
part 'communities_tree_selection_state.dart';

class CommunitiesTreeSelectionBloc
    extends Bloc<CommunitiesTreeSelectionEvent, CommunitiesTreeSelectionState> {
  CommunitiesTreeSelectionBloc() : super(const CommunitiesTreeSelectionState()) {
    on<SelectCommunityEvent>(_onSelectCommunity);
    on<SelectSpaceEvent>(_onSelectSpace);
    on<ClearCommunitiesTreeSelectionEvent>(_onClearSelection);
  }

  void _onSelectCommunity(
    SelectCommunityEvent event,
    Emitter<CommunitiesTreeSelectionState> emit,
  ) {
    emit(
      CommunitiesTreeSelectionState(
        selectedCommunity: event.community,
        selectedSpace: null,
      ),
    );
  }

  void _onSelectSpace(
    SelectSpaceEvent event,
    Emitter<CommunitiesTreeSelectionState> emit,
  ) {
    emit(
      CommunitiesTreeSelectionState(
        selectedCommunity: event.community,
        selectedSpace: event.space,
      ),
    );
  }

  void _onClearSelection(
    ClearCommunitiesTreeSelectionEvent event,
    Emitter<CommunitiesTreeSelectionState> emit,
  ) {
    emit(const CommunitiesTreeSelectionState());
  }
}
