import 'package:starting_template/domain/data_sources/local/counter_data_source.dart';
import 'package:starting_template/domain/entities/integer.dart';
import 'package:starting_template/domain/repositories/counter_repository.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';

class CounterRepositoryImpl extends CounterRepository {
  CounterRepositoryImpl({
    required CounterDataSource counterDataSource,
  }) : _counterDataSource = counterDataSource;

  final CounterDataSource _counterDataSource;

  @override
  FutureOrResult<Integer> decrement({Integer by = const Integer(1)}) =>
      Result.tryCatchAsync<Integer, AppException, AppException>(
        () async => _counterDataSource.decrement(by),
        (error) => error,
      );

  @override
  FutureOrResult<Integer> increment({Integer by = const Integer(1)}) =>
      Result.tryCatchAsync<Integer, AppException, AppException>(
        () async => _counterDataSource.increment(by),
        (error) => error,
      );

  @override
  FutureOrResult<Integer> getCurrent() =>
      Result.tryCatchAsync<Integer, AppException, AppException>(
        () async => _counterDataSource.getCurrent(),
        (error) => error,
      );

  @override
  FutureOrResult<Integer> reset() =>
      Result.tryCatchAsync<Integer, AppException, AppException>(
        () async => _counterDataSource.reset(),
        (error) => error,
      );
}
