import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_clean_code/domain/usecases/counter/decrement_counter.dart';
import 'package:wyatt_clean_code/domain/usecases/counter/increment_counter.dart';

class CounterCubit extends Cubit<int> {
  final IncrementCounter _incrementCounter;
  final DecrementCounter _decrementCounter;

  CounterCubit({
    required IncrementCounter incrementCounter,
    required DecrementCounter decrementCounter,
  })  : _incrementCounter = incrementCounter,
        _decrementCounter = decrementCounter,
        super(0);

  Future<void> increment({int by = 1}) async {
    // Use `.call(...)` to get documentation, but we can
    // also directly use `(...)`
    final response = await _incrementCounter.call(state + by);
    emit(
      response.fold(
        (value) => value,
        (error) => state,
      ),
    );
  }

  Future<void> decrement({int by = 1}) async {
    // Use `.call(...)` to get documentation, but we can
    // also directly use `(...)`
    final response = await _decrementCounter.call(state - by);
    emit(
      response.fold(
        (value) => value,
        (error) => state,
      ),
    );
  }
}
