export class CondensedGraphStats {
  constructor(graph) {
    this.graph = graph;
  }

  getStats() {
    const nodeHeadsBytes = this.graph.nodeHeads ? this.graph.nodeHeads.byteLength : 0;
    const adjacencyBytes = this.graph.adjacency ? this.graph.adjacency.byteLength : 0;
    const edgeArrayBytes = this.graph.edgeSrcIds
      ? this.graph.edgeSrcIds.byteLength
        + this.graph.edgeRelIds.byteLength
        + this.graph.edgeDstIds.byteLength
        + this.graph.edgePossibilityBits.byteLength
        + this.graph.edgeReliabilityBits.byteLength
      : 0;
    const typedArrayBytes = adjacencyBytes + edgeArrayBytes + this.graph.nodeDegrees.byteLength + nodeHeadsBytes + this.graph.values.byteLength;
    const nodeMapBytes = this.graph.nodeIds
      ? this.graph.nodeIds.length * 8 + this.graph.relationNameToId.size * 24
      : this.graph.relationNameToId.size * 24;
    let waveletBytes = 0;
    if (this.graph._waveletAll) {
      waveletBytes += this.graph._waveletAll.getMemoryUsage();
    }
    if (this.graph._waveletByRelId) {
      for (const wavelet of this.graph._waveletByRelId) {
        if (wavelet) waveletBytes += wavelet.getMemoryUsage();
      }
    }
    
    let avgDeg = 0;
    for (let i = 0; i < this.graph.numNodes; i++) {
      avgDeg += this.graph.nodeDegrees[i];
    }
    if (this.graph.numNodes > 0) {
      avgDeg = avgDeg / this.graph.numNodes;
    }
    
    const capacity = this.graph.adjacency
      ? this.graph.adjacency.length / 6
      : this.graph.edgeSrcIds
        ? this.graph.edgeSrcIds.length
        : 0;
    const utilization = capacity ? this.graph.numEdges / capacity : 0;

    return {
      numNodes: this.graph.numNodes,
      numEdges: this.graph.numEdges,
      maxDegree: this.graph.maxDegree,
      avgDegree: avgDeg,
      capacity,
      utilization,
      memoryUsage: {
        typedArrays: typedArrayBytes,
        nodeMap: nodeMapBytes,
        waveletAdjacency: waveletBytes,
        total: typedArrayBytes + nodeMapBytes + waveletBytes
      },
      bytesPerEdge: (typedArrayBytes + nodeMapBytes + waveletBytes) / (this.graph.numEdges || 1)
    };
  }
}
