import '../l10n/generated/app_localizations.dart';
import '../src/rust/lib.dart' as rust_err;

/// Map a `voiceJoin` error to a localized user-facing message.
String channelJoinErrorMessage(AppL10n l10n, Object error) {
  if (error is rust_err.BridgeError) {
    return error.when(
      invalidCommand: (msg) => l10n.channelJoinFailedGeneric(msg),
      dnsFailed: (host, reason) =>
          l10n.channelJoinFailedGeneric('$host: $reason'),
      connection: (msg) => l10n.channelJoinFailedGeneric(msg),
      notConnected: () => l10n.channelJoinFailedGeneric('not connected'),
      alreadyConnected: () =>
          l10n.channelJoinFailedGeneric('already connected'),
      serverRejected: (code, message) {
        switch (code) {
          case 0x0001:
            return l10n.channelJoinFailedTimeout;
          case 0x0a08:
            return l10n.channelJoinFailedPermission;
          case 0x0302:
            return l10n.channelJoinAlreadyIn;
          case 0x030d:
            return l10n.channelJoinFailedPassword;
          case 0x0309:
            return l10n.channelJoinFailedFull;
          case 0x030a:
            return l10n.channelJoinFailedFamilyFull;
          case 0x030e:
            return l10n.channelJoinFailedPrivate;
          default:
            return l10n.channelJoinFailedGeneric(message);
        }
      },
      unmapped: (msg) => l10n.channelJoinFailedGeneric(msg),
    );
  }
  return l10n.channelJoinFailedGeneric(error.toString());
}
