export class FileSize {
  private static readonly MAX_SIZE = BigInt(2 * 1024 * 1024 * 1024) // 2GB

  private constructor(private readonly _bytes: bigint) {}

  static create(bytes: bigint | number): FileSize {
    const bytesValue = typeof bytes === 'number' ? BigInt(bytes) : bytes
    if (bytesValue < 0n) {
      throw new Error('File size cannot be negative')
    }
    if (bytesValue > FileSize.MAX_SIZE) {
      throw new Error('File size cannot exceed 2GB')
    }
    return new FileSize(bytesValue)
  }

  get bytes(): bigint {
    return this._bytes
  }

  toFriendlyString(): string {
    const bytesNum = Number(this._bytes)
    if (bytesNum < 1024) return `${bytesNum} B`
    if (bytesNum < 1024 * 1024) return `${(bytesNum / 1024).toFixed(1)} KB`
    if (bytesNum < 1024 * 1024 * 1024) return `${(bytesNum / (1024 * 1024)).toFixed(1)} MB`
    return `${(bytesNum / (1024 * 1024 * 1024)).toFixed(2)} GB`
  }

  equals(other: FileSize): boolean {
    return this._bytes === other._bytes
  }
}
