<?php

declare(strict_types=1);

namespace PhpWA\Crypto\Signal;

use InvalidArgumentException;

final class SignalSessionIndex
{
    public const BASE_OURS = 1;
    public const BASE_THEIRS = 2;

    /** @var string */ private $baseKey;
    /** @var int */ private $baseKeyType;
    /** @var string */ private $remoteIdentityKey;
    /** @var int */ private $closed;

    public function __construct(string $baseKey, int $baseKeyType, string $remoteIdentityKey, int $closed = -1)
    {
        SignalCurve::assertPublicKey($baseKey);
        SignalCurve::assertPublicKey($remoteIdentityKey);
        if (($baseKeyType !== self::BASE_OURS && $baseKeyType !== self::BASE_THEIRS) || $closed < -1) {
            throw new InvalidArgumentException('Signal session index is invalid.');
        }
        $this->baseKey = $baseKey;
        $this->baseKeyType = $baseKeyType;
        $this->remoteIdentityKey = $remoteIdentityKey;
        $this->closed = $closed;
    }

    public function baseKey(): string { return $this->baseKey; }
    public function baseKeyType(): int { return $this->baseKeyType; }
    public function remoteIdentityKey(): string { return $this->remoteIdentityKey; }
    public function closed(): int { return $this->closed; }
    public function close(): void { if ($this->closed === -1) { $this->closed = 0; } }
}
