<?php

declare(strict_types=1);

namespace PhpWA\Transport\WebSocket;

use InvalidArgumentException;

final class WebSocketClientOptions
{
    /** @var string */ public $url;
    /** @var string */ public $origin;
    /** @var float */ public $connectTimeout;
    /** @var int */ public $maxMessageBytes;
    /** @var int */ public $maxHeaderBytes;
    /** @var bool */ public $allowInsecure;

    public function __construct(
        string $url = 'wss://web.whatsapp.com/ws/chat',
        string $origin = 'https://web.whatsapp.com',
        float $connectTimeout = 20.0,
        int $maxMessageBytes = 16777215,
        int $maxHeaderBytes = 16384,
        bool $allowInsecure = false
    ) {
        if (!is_finite($connectTimeout) || $connectTimeout <= 0.0 || $maxMessageBytes < 1 || $maxHeaderBytes < 1024) {
            throw new InvalidArgumentException('Invalid WebSocket client limits.');
        }
        if (preg_match('/[\x00-\x1f\x7f]/', $origin)) {
            throw new InvalidArgumentException('WebSocket Origin contains control characters.');
        }
        $this->url = $url;
        $this->origin = $origin;
        $this->connectTimeout = $connectTimeout;
        $this->maxMessageBytes = $maxMessageBytes;
        $this->maxHeaderBytes = $maxHeaderBytes;
        $this->allowInsecure = $allowInsecure;
    }
}
