import 'package:wyatt_clean_code/core/enums/exception_type.dart';
import 'package:wyatt_clean_code/core/errors/exceptions.dart';
import 'package:wyatt_clean_code/domain/repositories/counter_repository.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';

class CounterRepositoryImpl implements CounterRepository {
  Result<int, AppException> _check(int value) =>
      Result.conditionalLazy<int, AppException>(
        value >= 0,
        () => value,
        () => ClientException(
          AppExceptionType.assertion,
          "Counter can't be negative!",
        ),
      );

  @override
  Future<Result<int, AppException>> decrement(int newState) async =>
      _check(newState);

  @override
  Future<Result<int, AppException>> increment(int newState) async =>
      _check(newState);
}
