<?php

declare(strict_types=1);

namespace PhpWA\Media;

use InvalidArgumentException;

final class MediaConnection
{
      private $auth;
      private $ttl;
      private $hosts;
     
    public function __construct(string $auth, int $ttl, array $hosts)
    {
        if ($auth === '' || strlen($auth) > 8192 || preg_match('/[\x00-\x1f\x7f]/', $auth) === 1
            || $ttl < 1 || $ttl > 86400 || $hosts === [] || count($hosts) > 32) {
            throw new InvalidArgumentException('Media connection metadata is invalid.');
        }
        $unique = [];
        foreach ($hosts as $host) {
            if (!is_string($host) || strlen($host) > 253
                || preg_match('/\A(?=.{1,253}\z)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}\z/D', $host) !== 1) {
                throw new InvalidArgumentException('Media connection hostname is invalid.');
            }
            $unique[strtolower($host)] = true;
        }
        $this->auth = $auth; $this->ttl = $ttl; $this->hosts = array_keys($unique);
    }
    public function auth(): string { return $this->auth; }
    public function ttl(): int { return $this->ttl; }
      public function hosts(): array { return $this->hosts; }
}
