<?php
namespace boru\ai\Stream;

class StreamCollector
{
    /** @var StreamEvent[] */
    protected $events = array();

    /** @var string */
    protected $textBuffer = '';

    public function add(StreamEvent $event)
    {
        $this->events[] = $event;

        if ($event->type() === StreamEvent::RESPONSE_OUTPUT_TEXT_DELTA && is_string($event->delta())) {
            $this->textBuffer .= $event->delta();
        }

        if ($event->type() === StreamEvent::RESPONSE_OUTPUT_TEXT_DONE && is_string($event->text()) && $event->text() !== '') {
            $this->textBuffer = $event->text();
        }

        return $this;
    }

    public function events()
    {
        return $this->events;
    }

    public function textBuffer()
    {
        return $this->textBuffer;
    }
}
