<?php

declare(strict_types=1);

namespace PhpWA\Protocol;

use InvalidArgumentException;

final class ProtocolVersion
{
    public const DEFAULT_PRIMARY = 2;
    public const DEFAULT_SECONDARY = 3000;
    public const DEFAULT_TERTIARY = 1043857760;

      private $primary;
      private $secondary;
      private $tertiary;

    public function __construct(int $primary, int $secondary, int $tertiary)
    {
        foreach ([$primary, $secondary, $tertiary] as $part) {
            if ($part < 0 || $part > 4294967295) {
                throw new InvalidArgumentException('WhatsApp protocol version parts must fit uint32.');
            }
        }
        $this->primary = $primary;
        $this->secondary = $secondary;
        $this->tertiary = $tertiary;
    }

    public static function current(): self
    {
        return new self(self::DEFAULT_PRIMARY, self::DEFAULT_SECONDARY, self::DEFAULT_TERTIARY);
    }

     
    public static function fromArray(array $parts): self
    {
        if (count($parts) !== 3 || !isset($parts[0], $parts[1], $parts[2])
            || !is_int($parts[0]) || !is_int($parts[1]) || !is_int($parts[2])) {
            throw new InvalidArgumentException('WhatsApp protocol version must contain exactly three integers.');
        }
        return new self($parts[0], $parts[1], $parts[2]);
    }

    public function primary(): int { return $this->primary; }
    public function secondary(): int { return $this->secondary; }
    public function tertiary(): int { return $this->tertiary; }

     
    public function toArray(): array { return [$this->primary, $this->secondary, $this->tertiary]; }
    public function dotted(): string { return $this->primary . '.' . $this->secondary . '.' . $this->tertiary; }
}
