/**
 * PartyId Value Object
 *
 * Identifier for MPC party (format: {userId}-{type})
 */

export class PartyId {
  private readonly _value: string;

  private constructor(value: string) {
    this._value = value;
  }

  get value(): string {
    return this._value;
  }

  static create(value: string): PartyId {
    if (!value || value.trim().length === 0) {
      throw new Error('PartyId cannot be empty');
    }
    // Format: {userId}-{type} e.g., user123-server
    const partyIdRegex = /^[\w]+-[\w]+$/;
    if (!partyIdRegex.test(value)) {
      throw new Error('Invalid PartyId format. Expected: {identifier}-{type}');
    }
    return new PartyId(value);
  }

  static fromComponents(identifier: string, type: string): PartyId {
    return PartyId.create(`${identifier}-${type}`);
  }

  equals(other: PartyId): boolean {
    return this._value === other._value;
  }

  getIdentifier(): string {
    const parts = this._value.split('-');
    return parts.slice(0, -1).join('-');
  }

  getType(): string {
    return this._value.split('-').pop() || '';
  }

  toString(): string {
    return this._value;
  }
}
