import 'package:graphql_flutter/graphql_flutter.dart';

interface class DisplayableException {
  final String message;

  DisplayableException(this.message);
}

class NetworkException implements Exception, DisplayableException {
  @override
  final String message;

  NetworkException([this.message = 'No internet connection']);

  @override
  String toString() {
    return message;
  }
}

class GraphQLException implements Exception, DisplayableException {
  @override
  final String message;

  GraphQLException(this.message);

  @override
  String toString() {
    return message;
  }
}

class UnknownException implements Exception, DisplayableException {
  @override
  final String message;

  UnknownException([this.message = 'Something went wrong']);

  @override
  String toString() {
    return message;
  }
}

Exception parseBackendError(dynamic error) {
  if (error is OperationException) {
    if (error.graphqlErrors.isNotEmpty) {
      return GraphQLException(
          error.graphqlErrors.firstOrNull?.message ?? 'GraphQL error occurred');
    }
    return NetworkException('Network error occurred');
  }
  return UnknownException();
}
