import { RacingDomainValidationError } from '../errors/RacingDomainError';
import type { IValueObject } from '@core/shared/domain';

export class CountryCode implements IValueObject<string> {
  private constructor(private readonly value: string) {}

  static create(value: string): CountryCode {
    if (!value || value.trim().length === 0) {
      throw new RacingDomainValidationError('Country code is required');
    }
    const trimmed = value.trim().toUpperCase();
    if (!/^[A-Z]{2,3}$/.test(trimmed)) {
      throw new RacingDomainValidationError('Country must be a valid ISO code (2-3 letters)');
    }
    return new CountryCode(trimmed);
  }

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

  equals(other: IValueObject<string>): boolean {
    return this.value === other.props;
  }

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