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

/**
 * 下载链接值对象
 */
export class DownloadUrl {
  private readonly _value: string;

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

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

  static create(value: string): DownloadUrl {
    if (!value || value.trim() === '') {
      throw new DomainException('DownloadUrl cannot be empty');
    }

    const trimmed = value.trim();

    // 验证 URL 格式
    try {
      const url = new URL(trimmed);
      // 只允许 http/https 协议
      if (!['http:', 'https:'].includes(url.protocol)) {
        throw new Error('Invalid protocol');
      }
    } catch (e) {
      throw new DomainException('DownloadUrl must be a valid HTTP/HTTPS URL');
    }

    return new DownloadUrl(trimmed);
  }

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

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