
import { callExplorerApi } from '../../core';


export interface NetworkStats {
  total_transactions: string;
  total_blocks: number;
  last_block_number: string;
  average_block_time: string;
  
  transactions_per_second: string;
  today_transactions: string;
  new_contracts: number;
  active_validators: number;
  avg_txn_per_block: string;
  success_rate: string;
  
  tx_change_percent: number;
  blocks_change_percent: number;
  block_time_change_percent: number;
  tx_per_block_change_percent: number;
}


export interface IndexerStatus {
  last_indexed_block: string;
  current_block: number;
  is_syncing: boolean;
  sync_percentage: number;
}


export interface NetworkStatsResponse {
  stats: NetworkStats;
  latest_blocks: any[];
  indexer: IndexerStatus;
}

/**
 * 
 * @returns 
 */
export async function getNetworkStats(): Promise<NetworkStatsResponse> {
  try {
    const response = await callExplorerApi<any>('stats');
    
    if (response && response.status === 'success' && response.data) {
      return response.data;
    }
    
    throw new Error('Invalid response format from explorer API');
  } catch (error) {
    console.error('Error fetching network stats:', error);
    throw error;
  }
}

/**
 * 
 * @param value 
 * @returns 
 */
export function formatNumber(value: string | number): string {
  if (value === null || value === undefined) return '0';
  
  const num = typeof value === 'string' ? parseFloat(value) : value;
  return num.toLocaleString();
}

/**
 * 
 * @param value 
 * @returns 
 */
export function formatPercentChange(value: number): string {
  if (value === null || value === undefined) return '0%';
  
  const sign = value > 0 ? '+' : '';
  return `${sign}${value.toFixed(1)}%`;
} 