import 'package:flutter_bloc/flutter_bloc.dart';
import 'community_dialog_event.dart';
import 'community_dialog_state.dart';

class CommunityDialogBloc extends Bloc<CommunityDialogEvent, CommunityDialogState> {
  final List<String> existingCommunityNames;

  CommunityDialogBloc(this.existingCommunityNames)
      : super(CommunityDialogInitial()) {
    on<ValidateCommunityNameEvent>((event, emit) {
      final trimmedName = event.name.trim();
      final isNameValid = !existingCommunityNames.contains(trimmedName);
      final isNameEmpty = trimmedName.isEmpty;

      emit(CommunityNameValidationState(
        isNameValid: isNameValid,
        isNameEmpty: isNameEmpty,
      ));
    });
  }
}
