/** Ack error codes → short, friendly copy for a phone in a loud bar. */

export interface AckErrorLike {
  code: string;
  message: string;
}

/** Codes that mean "this phone's player is gone": clear tokens and send the guest back to join. */
const REJOIN_CODES: ReadonlySet<string> = new Set(['UNAUTHORIZED', 'NO_ACTIVE_SESSION', 'NOT_FOUND']);

export function needsRejoin(code: string): boolean {
  return REJOIN_CODES.has(code);
}

/** Codes worth retrying automatically (the connection layer already retries these for idempotent calls). */
export function isTransient(code: string): boolean {
  return code === 'OFFLINE' || code === 'TIMEOUT';
}

export function friendlyError(error: AckErrorLike): string {
  switch (error.code) {
    case 'RATE_LIMITED':
      return 'Whoa, slow down — too many taps. Try again in a second.';
    case 'OFFLINE':
      return 'You’re offline. Reconnecting — try again in a moment.';
    case 'TIMEOUT':
      return 'The game didn’t answer. Check your Wi-Fi and try again.';
    case 'TOO_LATE':
      return 'Too late — answers were already locked.';
    case 'ALREADY_ANSWERED':
      return 'You already locked in an answer.';
    case 'WRONG_PHASE':
    case 'WRONG_QUESTION':
      return 'That moment has passed — keep your eyes on the next one.';
    case 'NOT_PARTICIPANT':
      return 'You’re not playing this round — you’ll jump in next round.';
    case 'INSUFFICIENT_BALANCE':
      return 'You need at least 100 points to Double Down.';
    case 'NO_TOKEN':
      return 'Complete a line to earn a power-up.';
    case 'POWERUP_ALREADY_USED':
      return 'You already used that power-up tonight.';
    case 'INTERNAL':
      return 'Something went wrong on our side. Try again.';
    default:
      return error.message || 'Something went wrong. Try again.';
  }
}
