export class Hashpower {
  private constructor(public readonly value: number) {
    if (value < 0) {
      throw new Error('算力不能为负数');
    }
  }

  static create(value: number): Hashpower {
    return new Hashpower(value);
  }

  static zero(): Hashpower {
    return new Hashpower(0);
  }

  /**
   * 根据树数量和百分比计算算力
   * @param treeCount 树数量
   * @param percent 算力百分比 (1 = 1%)
   */
  static fromTreeCount(treeCount: number, percent: number): Hashpower {
    return new Hashpower(treeCount * percent);
  }

  add(other: Hashpower): Hashpower {
    return new Hashpower(this.value + other.value);
  }

  subtract(other: Hashpower): Hashpower {
    return new Hashpower(Math.max(0, this.value - other.value));
  }

  equals(other: Hashpower): boolean {
    return this.value === other.value;
  }

  isZero(): boolean {
    return this.value === 0;
  }
}
