/**
 * Threshold Value Object
 *
 * MPC threshold configuration (t-of-n)
 */

export class Threshold {
  private readonly _n: number; // Total number of parties
  private readonly _t: number; // Minimum required signers

  private constructor(n: number, t: number) {
    this._n = n;
    this._t = t;
  }

  get n(): number {
    return this._n;
  }

  get t(): number {
    return this._t;
  }

  static create(n: number, t: number): Threshold {
    if (!Number.isInteger(n) || !Number.isInteger(t)) {
      throw new Error('Threshold values must be integers');
    }
    if (n <= 0 || t <= 0) {
      throw new Error('Threshold values must be positive');
    }
    if (t > n) {
      throw new Error('t cannot exceed n');
    }
    if (t < 2) {
      throw new Error('t must be at least 2 for security');
    }
    return new Threshold(n, t);
  }

  /**
   * Common threshold configurations
   */
  static twoOfThree(): Threshold {
    return new Threshold(3, 2);
  }

  static threeOfFive(): Threshold {
    return new Threshold(5, 3);
  }

  /**
   * Validate if participant count meets threshold requirements
   */
  validateParticipants(participantsCount: number): boolean {
    return participantsCount >= this._t && participantsCount <= this._n;
  }

  equals(other: Threshold): boolean {
    return this._n === other._n && this._t === other._t;
  }

  toString(): string {
    return `${this._t}-of-${this._n}`;
  }
}
