export interface AppError {
  message: string
  code?: string
  details?: any
  retryable?: boolean
}

export function formatError(error: unknown): AppError {
  if (error instanceof Error) {
    return {
      message: error.message,
      details: error.stack,
      retryable: false,
    }
  }

  if (typeof error === 'string') {
    return {
      message: error,
      retryable: false,
    }
  }

  if (error && typeof error === 'object' && 'message' in error) {
    return {
      message: String(error.message),
      code: 'code' in error ? String(error.code) : undefined,
      details: error,
      retryable: 'retryable' in error ? Boolean(error.retryable) : false,
    }
  }

  return {
    message: 'An unknown error occurred',
    retryable: false,
  }
}

export function getErrorMessage(error: unknown): string {
  const formatted = formatError(error)
  return formatted.message
}

export function isRetryable(error: unknown): boolean {
  const formatted = formatError(error)
  return formatted.retryable || false
}

