import 'failures.dart';

sealed class Result<T> {
  const Result();

  bool get isSuccess => this is Success<T>;
  bool get isFailure => this is Failure;

  T? get data => this is Success<T> ? (this as Success<T>).data : null;
  Failure? get failure => this is Error<T> ? (this as Error<T>).failure : null;

  R when<R>({
    required R Function(T data) success,
    required R Function(Failure failure) error,
  }) {
    if (this is Success<T>) {
      return success((this as Success<T>).data);
    } else {
      return error((this as Error<T>).failure);
    }
  }
}

class Success<T> extends Result<T> {
  @override
  final T data;

  const Success(this.data);
}

class Error<T> extends Result<T> {
  @override
  final Failure failure;

  const Error(this.failure);
}
