<?php

/*
 * This file is part of the official PHP MCP SDK.
 *
 * A collaboration between Symfony and the PHP Foundation.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Matomo\Dependencies\McpServer\Mcp\Server\Transport;

use Psr\Log\LoggerInterface;
use Matomo\Dependencies\McpServer\Symfony\Component\Uid\Uuid;
/**
 * @extends BaseTransport<null>
 *
 * @author Tobias Nyholm <tobias.nyholm@gmail.com>
 */
class InMemoryTransport extends BaseTransport
{
    /**
     * @param list<string> $messages
     */
    public function __construct(private readonly array $messages = [], ?LoggerInterface $logger = null)
    {
        parent::__construct($logger);
    }
    public function onMessage(callable $listener) : void
    {
        $this->messageListener = $listener;
    }
    public function send(string $data, array $context) : void
    {
        if (isset($context['session_id'])) {
            $this->sessionId = $context['session_id'];
        }
    }
    /**
     * @return null
     */
    public function listen() : mixed
    {
        $this->logger->info('InMemoryTransport is processing messages...');
        foreach ($this->messages as $message) {
            $this->handleMessage($message, $this->sessionId);
        }
        $this->logger->info('InMemoryTransport finished processing.');
        $this->handleSessionEnd($this->sessionId);
        $this->sessionId = null;
        return null;
    }
    public function setSessionId(?Uuid $sessionId) : void
    {
        $this->sessionId = $sessionId;
    }
    public function close() : void
    {
        $this->handleSessionEnd($this->sessionId);
        $this->sessionId = null;
    }
}
