import 'package:bloc/bloc.dart';
import 'text_event.dart';
import 'text_state.dart';

class TextBloc extends Bloc<TextEvent, TextState> {
  final int maxCharacters;

  TextBloc(this.maxCharacters) : super(TextInitial()) {
    on<TextChanged>(_onTextChanged);
  }

  void _onTextChanged(TextChanged event, Emitter<TextState> emit) {
    final charactersLeft = maxCharacters - event.text.length;
    emit(TextUpdated(event.text, charactersLeft));
  }
}
