export enum Libp2pError {
  // https://github.com/libp2p/js-libp2p/blob/b936324a92038d9447983bb11db623c1b5a17a76/packages/libp2p/src/errors.ts#L6
  MissingServiceError = "MissingServiceError",
  UnmetServiceDependenciesError = "UnmetServiceDependenciesError",
  NoContentRoutersError = "NoContentRoutersError",
  NoPeerRoutersError = "NoPeerRoutersError",
  QueriedForSelfError = "QueriedForSelfError",
  UnhandledProtocolError = "UnhandledProtocolError",
  DuplicateProtocolHandlerError = "DuplicateProtocolHandlerError",
  DialDeniedError = "DialDeniedError",
  UnsupportedListenAddressError = "UnsupportedListenAddressError",
  UnsupportedListenAddressesError = "UnsupportedListenAddressesError",
  NoValidAddressesError = "NoValidAddressesError",
  ConnectionInterceptedError = "ConnectionInterceptedError",
  ConnectionDeniedError = "ConnectionDeniedError",
  MuxerUnavailableError = "MuxerUnavailableError",
  EncryptionFailedError = "EncryptionFailedError",
  TransportUnavailableError = "TransportUnavailableError",
  // https://github.com/libp2p/js-libp2p/blob/b936324a92038d9447983bb11db623c1b5a17a76/packages/interface/src/errors.ts#L6
  AbortError = "AbortError",
  UnexpectedPeerError = "UnexpectedPeerError",
  InvalidCryptoExchangeError = "InvalidCryptoExchangeError",
  InvalidParametersError = "InvalidParametersError",
  InvalidPublicKeyError = "InvalidPublicKeyError",
  InvalidPrivateKeyError = "InvalidPrivateKeyError",
  UnsupportedOperationError = "UnsupportedOperationError",
  ConnectionClosingError = "ConnectionClosingError",
  ConnectionClosedError = "ConnectionClosedError",
  ConnectionFailedError = "ConnectionFailedError",
  MuxerClosedError = "MuxerClosedError",
  StreamResetError = "StreamResetError",
  StreamStateError = "StreamStateError",
  NotFoundError = "NotFoundError",
  InvalidPeerIdError = "InvalidPeerIdError",
  InvalidMultiaddrError = "InvalidMultiaddrError",
  InvalidCidError = "InvalidCIDError",
  InvalidMultihashError = "InvalidMultihashError",
  UnsupportedProtocolError = "UnsupportedProtocolError",
  InvalidMessageError = "InvalidMessageError",
  ProtocolError = "ProtocolError",
  TimeoutError = "TimeoutError",
  NotStartedError = "NotStartedError",
  AlreadyStartedError = "AlreadyStartedError",
  DialError = "DialError",
  ListenError = "ListenError",
  LimitedConnectionError = "LimitedConnectionError",
  TooManyInboundProtocolStreamsError = "TooManyInboundProtocolStreamsError",
  TooManyOutboundProtocolStreamsError = "TooManyOutboundProtocolStreamsError",
  UnsupportedKeyTypeError = "UnsupportedKeyTypeError",
  OtherError = "OtherError",
}

export function getLibp2pError(error: Error): Libp2pError {
  return Libp2pError[(error as unknown as {name: string}).name as keyof typeof Libp2pError] ?? Libp2pError.OtherError;
}
