import { DomainException } from '../../shared/exceptions/domain.exception';

/**
 * 最低操作系统版本值对象
 */
export class MinOsVersion {
  private readonly _value: string;

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

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

  static create(value: string): MinOsVersion {
    // 允许空值，返回默认版本
    if (!value || value.trim() === '') {
      return MinOsVersion.none();
    }

    const trimmed = value.trim();

    // 支持纯数字格式 (e.g., "24") 自动转换为 "24.0"
    if (/^\d+$/.test(trimmed)) {
      return new MinOsVersion(`${trimmed}.0`);
    }

    // 验证格式: 数字.数字 或 数字.数字.数字 (e.g., "11.0", "14.0", "5.1.1")
    if (!/^\d+(\.\d+){1,2}$/.test(trimmed)) {
      throw new DomainException('MinOsVersion must be in format like "11.0" or "5.1.1"');
    }

    return new MinOsVersion(trimmed);
  }

  static none(): MinOsVersion {
    return new MinOsVersion('0.0');
  }

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

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