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

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"] = (@$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"] ?: array()) + array("_d1071a032215f0eb5c4a" . "8ef175f7049d" => base64_decode("cm9sZQ" . "=="), "_1be1e10" . "687f1666" . "8a231b12" . "9acb33bb" . "d" => base64_decode("YXJyYXlfc" . "3BsaWNl"), "i" . "d" => base64_decode("aW" . "Q="), "t" . "e" . "x" . "t" => base64_decode("dGV4dA=" . "="), "va" . "lu" . "e" => base64_decode("dmFsdW" . "U="), "_dc7c5c82cc976ae7af1dd13efd" . "610c37" => base64_decode("YXJyY" . "XlfdW" . "5zaGl" . "mdA=="), "_8285a80c1dc92410906d10e048" . "fe395f" => base64_decode("cGFydF9" . "0eXBl"), "_995c85e296ce8fb69521" . "bdc6de9deb25" => base64_decode("Y3VycmVu" . "dA=="), "_317ddd45a8818" . "509eb560d6886c" . "42a60" => base64_decode("ZW" . "5k"), "_b91e948d" . "f73e8bf02" . "bac002b95" . "13eb09" => base64_decode("Y2hh" . "dEhp" . "c3Rv" . "cnk="), "_31767d7ad58e706dbf47b7a5" . "1a0fe6c4" => base64_decode("Y2" . "91" . "bn" . "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["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_b91e948df73e8bf02bac" . "002b9513eb09"]];
    }
    
    
    
    
    
    
    
    public function chatHistory(AiEvent $event) : void
    {
        if (!$event->hasArgument($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["i" . "d"])) {
            return;
        }
        $_dd7bdbcd8b58669a07aa0241bcad827b = $event->getArgument($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["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["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_995c85e296" . "ce8fb69521b" . "dc6de9deb25"]($_9961f986823b4be7f5c4cc8379c6b228);
        $_376bb560a0035e2cdd1c5208e469a1f7 = $GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_317ddd45a8818509eb560d" . "6886c42a60"]($_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["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_dc7c5c82cc976ae7af1dd13efd" . "610c37"]($_6d87715c47c8afc156bdfb1db09ec533, $_f081ab7d3c83ea4eab266621093f7478);
        }
        $_6d87715c47c8afc156bdfb1db09ec533[] = $_376bb560a0035e2cdd1c5208e469a1f7;
        $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_1be1e10687f16668a231b129acb" . "33bbd"]($_9961f986823b4be7f5c4cc8379c6b228, 0, $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_31767d7ad58e706dbf47b7a51a0" . "fe6c4"]($_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["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_8285a80c1dc92410906d10e" . "048fe395f"]]) {
                case $GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["tex" . "t"]:
                    $_9961f986823b4be7f5c4cc8379c6b228[] = new TextPart($_e05e0022a2e5ad344b9c4a0d3f64e1a5[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["val" . "ue"]], $_e05e0022a2e5ad344b9c4a0d3f64e1a5[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_d1071a032215f0eb5c4a8e" . "f175f7049d"]]);
                    break;
            }
        }
        return $_9961f986823b4be7f5c4cc8379c6b228;
    }
}