<?php
namespace Claromentis\Core\Ai\Subscriber;

$GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] ?: array()) + array("_b91e948df73e8bf02bac002b9513" . "eb09" => base64_decode("Y2hhdEhpc3" . "Rvcnk="), "_995c85e296ce8fb69521bdc6de9d" . "eb25" => base64_decode("Y3Vyc" . "mVudA" . "=="), "i" . "d" => base64_decode("aWQ" . "="), "_31767d7ad58e706dbf47b7a51a0f" . "e6c4" => base64_decode("Y291" . "bnQ="), "_1be1e10687f16668a231b129acb" . "33bbd" => base64_decode("YXJyYXlf" . "c3BsaWNl"), "_8285a80c" . "1dc924109" . "06d10e048" . "fe395f" => base64_decode("cGFydF90" . "eXBl"), "_dc7c5c82cc976ae7af1dd13efd6" . "10c37" => base64_decode("YXJyYXlfdW5zaGlmd" . "A=="), "tex" . "t" => base64_decode("dGV4dA" . "=="), "_317ddd45a88" . "18509eb560d6" . "886c42a60" => base64_decode("ZW5" . "k"), "val" . "ue" => base64_decode("dmFsd" . "WU="), "_d1071a032215f0eb5c4a8ef175f7049" . "d" => base64_decode("cm9sZ" . "Q=="));
use Claromentis\Core\Ai\Entity\AiChatHistory;
use Claromentis\Core\Ai\Events\AiEvent;
use Claromentis\Core\Ai\Events\AiEventType;
use Claromentis\Core\Ai\Parts\FunctionCallPart;
use Claromentis\Core\Ai\Parts\PartInterface;
use Claromentis\Core\Ai\Parts\TextPart;
use Claromentis\Core\Ai\Repository\AiChatRepository;
use Claromentis\Core\Security\SecurityContext;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ChatHistorySubscriber implements EventSubscriberInterface
{
    private AiChatRepository $repository;
    private SecurityContext $context;
    public function __construct(AiChatRepository $repository, SecurityContext $context, EventDispatcherInterface $_b497ce6f2af10f67516aed0ecf34d8cf)
    {
        $this->repository = $repository;
        $this->context = $context;
    }
    
    
    
    
    
    
    
    
    public static function getSubscribedEvents() : array
    {
        return [AiEventType::CHAT_PROMPT => $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_b91e948df73e8bf0" . "2bac002b9513eb09"]];
    }
    
    
    
    
    
    
    
    public function chatHistory(AiEvent $event) : void
    {
        if (!$event->hasArgument($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["i" . "d"])) {
            return;
        }
        $_dd7bdbcd8b58669a07aa0241bcad827b = $event->getArgument($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["i" . "d"]);
        if (!$_dd7bdbcd8b58669a07aa0241bcad827b) {
            return;
        }
        $_dace5e187fa775d4aabf1747566801d7 = $this->repository->getChatWithHistory($this->context, $_dd7bdbcd8b58669a07aa0241bcad827b);
        if (!$_dace5e187fa775d4aabf1747566801d7) {
            return;
        }
        $_a82e7c9cba13670954fbe01d604c242a = clone $_dace5e187fa775d4aabf1747566801d7->history;
        $_9961f986823b4be7f5c4cc8379c6b228 = $event->getParts();
        if (empty($_9961f986823b4be7f5c4cc8379c6b228)) {
            return;
        }
        $_f081ab7d3c83ea4eab266621093f7478 = $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_995c85e296ce8" . "fb69521bdc6de9" . "deb25"]($_9961f986823b4be7f5c4cc8379c6b228);
        $_376bb560a0035e2cdd1c5208e469a1f7 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_317ddd4" . "5a881850" . "9eb560d6" . "886c42a6" . "0"]($_9961f986823b4be7f5c4cc8379c6b228);
        if ($_376bb560a0035e2cdd1c5208e469a1f7 instanceof FunctionCallPart) {
            return;
        }
        $this->repository->addHistory($this->context, $_dace5e187fa775d4aabf1747566801d7, $_376bb560a0035e2cdd1c5208e469a1f7);
        if (!$_376bb560a0035e2cdd1c5208e469a1f7->getRole() === PartInterface::PART_ROLE_MODEL) {
            return;
        }
        $_6d87715c47c8afc156bdfb1db09ec533 = $this->mapToParts($_a82e7c9cba13670954fbe01d604c242a->toArray());
        if ($_f081ab7d3c83ea4eab266621093f7478 !== $_376bb560a0035e2cdd1c5208e469a1f7 && $_f081ab7d3c83ea4eab266621093f7478->getRole() === PartInterface::PART_ROLE_MODEL) {
            $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_dc7c5c82cc976ae7af1dd13efd610" . "c37"]($_6d87715c47c8afc156bdfb1db09ec533, $_f081ab7d3c83ea4eab266621093f7478);
        }
        $_6d87715c47c8afc156bdfb1db09ec533[] = $_376bb560a0035e2cdd1c5208e469a1f7;
        $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_1be1e10687f16" . "668a231b129acb" . "33bbd"]($_9961f986823b4be7f5c4cc8379c6b228, 0, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_31767d7" . "ad58e706" . "dbf47b7a" . "51a0fe6c" . "4"]($_9961f986823b4be7f5c4cc8379c6b228), $_6d87715c47c8afc156bdfb1db09ec533);
        $event->setParts($_9961f986823b4be7f5c4cc8379c6b228);
        if (!$_dace5e187fa775d4aabf1747566801d7->title) {
            $this->repository->updateTitle($_dace5e187fa775d4aabf1747566801d7, $_376bb560a0035e2cdd1c5208e469a1f7->getValue());
        }
    }
    
    
    
    
    
    
    
    private function mapToParts($_fab5a2188c706d282c3aea92f0ccbd01) : array
    {
        $_9961f986823b4be7f5c4cc8379c6b228 = [];
        foreach ($_fab5a2188c706d282c3aea92f0ccbd01 as $_e05e0022a2e5ad344b9c4a0d3f64e1a5) {
            switch ($_e05e0022a2e5ad344b9c4a0d3f64e1a5[$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_8285a80" . "c1dc9241" . "0906d10e" . "048fe395" . "f"]]) {
                case $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["tex" . "t"]:
                    $_9961f986823b4be7f5c4cc8379c6b228[] = new TextPart($_e05e0022a2e5ad344b9c4a0d3f64e1a5[$GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["va" . "lu" . "e"]], $_e05e0022a2e5ad344b9c4a0d3f64e1a5[$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_d1071a032215f0eb5c4a8e" . "f175f7049d"]]);
                    break;
            }
        }
        return $_9961f986823b4be7f5c4cc8379c6b228;
    }
}