<?php
namespace Claromentis\Core\Ai;

$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"] ?: array()) + array("_bd71c3b5c201fc11db82ed14ce" . "424de5" => base64_decode("aW5fY" . "XJyYX" . "k="), "_e7b54270ed40a301349cf7f87" . "be1de75" => base64_decode("CltBREhFUkUgVE8gVEhFU0UgQURESVRJT05BTCBVU0VSIFJVTEVTXToKLS" . "A="), "_e65d9ebe03c7b68f0359" . "7f9bf0f297d8" => base64_decode("RWFjaCBlbGVtZW50IG11c3QgYmUgYW4gaW5zdGFuY2Ugb2" . "Yg"), "_a70b8baf1a135547" . "a27100200b31ba5e" => base64_decode("c29" . "ydA" . "=="), "_24124167882dd54c5fd27989982de7" . "07" => base64_decode("QUkgcGx1Z2lucyBtdXN0IGlt" . "cGxlbWVudCA="), "_ea8229a12128" . "5ee649a72fd88" . "0bac39b" => base64_decode("YXJyYXlfZml" . "sdGVy"), "_34725a20914" . "b07a28de7045" . "ef89ffe6e" => base64_decode("SW52YWxpZCBwbHVna" . "W4gdHlwZQ=="), "_4298577e41894bcce23f896cd36" . "5aea4" => base64_decode("ZW4" . "="), "_38f12d8b8c719a8ce0f" . "c2faefc225f35" => base64_decode("YXJyYXl" . "fbWFw"), "_d2c06c4cf8a9682c7564425386" . "57aa7e" => base64_decode("cHJvb" . "XB0cw" . "=="), "_2e384775c" . "6854f15e61" . "3f8da88db6" . "08d" => base64_decode("Y2ZnX2FpX2VuYWJsZWRfbW9kdWx" . "lcw=="), "_babfb4f2cba0a2822e0d05c66d8b6" . "762" => base64_decode("Ci" . "0g"), "_075c6c41" . "72eaf58cf" . "a84828a35" . "82d19a" => base64_decode("YXJy" . "YXlf" . "dW5p" . "cXVl"), "_5db6d0093c6a62ddf3a9e390c17" . "191ae" => base64_decode("dHJpbQ" . "=="), "_5159b3572" . "969eca46ac" . "d1b2e14184" . "59c" => base64_decode("aW1wbG9kZ" . "Q=="));
use Claromentis\Core\Ai\Events\AiEvent;
use Claromentis\Core\Ai\Events\AiEventType;
use Claromentis\Core\Ai\Exceptions\AiInvalidRequestException;
use Claromentis\Core\Ai\Parts\PartInterface;
use Claromentis\Core\Ai\Parts\TextPart;
use Claromentis\Core\Ai\Repository\CustomPromptRepository;
use Claromentis\Core\Ai\Util\Util;
use Claromentis\Core\Config\Config;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webmozart\Assert\Assert;




class Ai implements AiAdapterInterface
{
    
    
    
    
    protected AiAdapterInterface $adapter;
    
    
    
    
    protected CustomPromptRepository $customRepository;
    protected Config $config;
    
    
    
    
    protected EventDispatcherInterface $dispatcher;
    
    
    
    
    
    
    protected array $plugins;
    
    
    
    
    
    
    
    
    public function __construct(AiAdapterInterface $_fff71005fefc432e322a63468ff7fd0c, CustomPromptRepository $_222dbf3ebab3159c15106353e8cbe2c2, Config $config, EventDispatcherInterface $_b497ce6f2af10f67516aed0ecf34d8cf, array $_a99e51514fd0fcca58579cb5552a6035 = [])
    {
        Assert::allIsInstanceOf($_a99e51514fd0fcca58579cb5552a6035, AiPluginInterface::class, $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_24124167882dd" . "54c5fd27989982" . "de707"] . AiPluginInterface::class);
        $this->adapter = $_fff71005fefc432e322a63468ff7fd0c;
        $this->customRepository = $_222dbf3ebab3159c15106353e8cbe2c2;
        $this->config = $config;
        $this->dispatcher = $_b497ce6f2af10f67516aed0ecf34d8cf;
        $this->plugins = $_a99e51514fd0fcca58579cb5552a6035;
    }
    public function generateText($_9961f986823b4be7f5c4cc8379c6b228, ?AiTextGenerationConfiguration $_91cc42f5289e2e63eeb6b3317a65abbd = null) : string
    {
        Util::assertTextGenerationParts($_9961f986823b4be7f5c4cc8379c6b228);
        $this->dispatchEvent(AiEventType::TEXT_PROMPT, $_9961f986823b4be7f5c4cc8379c6b228);
        return $this->adapter->generateText($_9961f986823b4be7f5c4cc8379c6b228, $_91cc42f5289e2e63eeb6b3317a65abbd);
    }
    public function generateChat(array $_9961f986823b4be7f5c4cc8379c6b228) : string
    {
        Assert::allIsInstanceOf($_9961f986823b4be7f5c4cc8379c6b228, PartInterface::class, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_e65d9ebe03c7b6" . "8f03597f9bf0f29" . "7d8"] . PartInterface::class);
        $this->dispatchEvent(AiEventType::CHAT_PROMPT, $_9961f986823b4be7f5c4cc8379c6b228);
        return $this->adapter->generateChat($_9961f986823b4be7f5c4cc8379c6b228);
    }
    public function generateTextStream(callable $_5c51b0e4b7267b43adabf8015a8e1cf8, $_9961f986823b4be7f5c4cc8379c6b228) : void
    {
        Util::assertTextGenerationParts($_9961f986823b4be7f5c4cc8379c6b228);
        $this->dispatchEvent(AiEventType::TEXT_PROMPT, $_9961f986823b4be7f5c4cc8379c6b228);
        $this->adapter->generateTextStream($_5c51b0e4b7267b43adabf8015a8e1cf8, $_9961f986823b4be7f5c4cc8379c6b228);
    }
    public function generateImages(string $_a51236f5e7f12cf84a776152098d24a9, ?AiImageConfiguration $_96a90e63b3679cfbc97e4f9bc15d4d2a = null) : array
    {
        $this->dispatchEvent(AiEventType::IMAGE_PROMPT, $_a51236f5e7f12cf84a776152098d24a9);
        return $this->adapter->generateImages($_a51236f5e7f12cf84a776152098d24a9, $_96a90e63b3679cfbc97e4f9bc15d4d2a);
    }
    public function generateTextEmbedding($_9961f986823b4be7f5c4cc8379c6b228) : array
    {
        Util::assertTextGenerationParts($_9961f986823b4be7f5c4cc8379c6b228);
        return $this->adapter->generateTextEmbedding($_9961f986823b4be7f5c4cc8379c6b228);
    }
    
    
    
    
    
    
    
    public function isModuleEnabled(string $module) : bool
    {
        return $this->config->has($GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_2e384775c6854f15e613f" . "8da88db608d"]) && ($this->config->get($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_2e384775c6854f15e613f8da8" . "8db608d"])[$module] ?? false);
    }
    
    
    
    
    
    
    public function getModules()
    {
        $_59eea0afb98cc6c83c49d730362a45fd = [];
        foreach ($this->plugins as $_38198dbb7190fed0f875632ecef1ac6f) {
            $_59eea0afb98cc6c83c49d730362a45fd[] = $_38198dbb7190fed0f875632ecef1ac6f->getModule();
        }
        $_59eea0afb98cc6c83c49d730362a45fd = $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_075c6c4172eaf58cfa84828a3582d19" . "a"]($GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_ea8229a121" . "285ee649a72" . "fd880bac39b"]($_59eea0afb98cc6c83c49d730362a45fd));
        $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_a70b8baf1a135547a" . "27100200b31ba5e"]($_59eea0afb98cc6c83c49d730362a45fd);
        return $_59eea0afb98cc6c83c49d730362a45fd;
    }
    
    
    
    
    
    
    
    public function getModulePlugins(string $module) : array
    {
        $_a99e51514fd0fcca58579cb5552a6035 = [];
        foreach ($this->plugins as $_38198dbb7190fed0f875632ecef1ac6f) {
            if ($_38198dbb7190fed0f875632ecef1ac6f->getModule() === $module) {
                $_a99e51514fd0fcca58579cb5552a6035[] = $_38198dbb7190fed0f875632ecef1ac6f;
            }
        }
        return $_a99e51514fd0fcca58579cb5552a6035;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function getSystemPromptParts(AiPluginInterface $_38198dbb7190fed0f875632ecef1ac6f, ?string $_a05cd0d81b0d993868f75f877961db96 = null, ?array $_b255ebafd5b8bb45110cd2db0604fa44 = null) : array
    {
        
        
        $_a05cd0d81b0d993868f75f877961db96 = $this->getSupportedLanguage($_a05cd0d81b0d993868f75f877961db96);
        $_9961f986823b4be7f5c4cc8379c6b228 = $_38198dbb7190fed0f875632ecef1ac6f->getSystemPromptParts($_a05cd0d81b0d993868f75f877961db96, $_b255ebafd5b8bb45110cd2db0604fa44);
        $data = $this->getCustomPrompts($_38198dbb7190fed0f875632ecef1ac6f->getModule());
        if (!empty($data[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_d2c06c4cf8a9682c7564425" . "38657aa7e"]])) {
            $_9961f986823b4be7f5c4cc8379c6b228[] = new TextPart($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_e7b54270ed40a301349cf7f8" . "7be1de75"] . $GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_5159b3572" . "969eca46ac" . "d1b2e14184" . "59c"]($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_babfb4f2cba0a2822e0d05c" . "66d8b6762"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_38f12d8b8c719a8c" . "e0fc2faefc225f35"]($GLOBALS["__65da1b1ec2d6653e8" . "a0ab7848e536215"]["_5db6d0093" . "c6a62ddf3a" . "9e390c1719" . "1ae"], $data[$GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_d2c06c4cf8a9682c756442538657a" . "a7e"]])));
        }
        return $_9961f986823b4be7f5c4cc8379c6b228;
    }
    
    
    
    
    
    
    
    
    public function getPlugin(string $module, string $_2236a44cb0a254e187e94523cbb4ccbb) : ?AiPluginInterface
    {
        if (!$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_bd71c3b5c201fc11db82ed" . "14ce424de5"]($_2236a44cb0a254e187e94523cbb4ccbb, AiPluginInterface::TYPES)) {
            throw new \InvalidArgumentException($GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_34725a20914b" . "07a28de7045ef" . "89ffe6e"]);
        }
        
        
        foreach ($this->plugins as $_38198dbb7190fed0f875632ecef1ac6f) {
            if ($_38198dbb7190fed0f875632ecef1ac6f->getModule() === $module && $_38198dbb7190fed0f875632ecef1ac6f->getType() === $_2236a44cb0a254e187e94523cbb4ccbb) {
                return $_38198dbb7190fed0f875632ecef1ac6f;
            }
        }
        return null;
    }
    
    
    
    
    
    
    
    public function getCustomPrompts(string $module) : array
    {
        return $this->customRepository->get($module);
    }
    
    
    
    
    
    
    
    
    public function saveCustomPrompts(string $module, array $data)
    {
        $this->customRepository->save($module, $data);
    }
    
    
    
    
    
    
    
    
    
    
    
    protected function getSupportedLanguage(?string $language) : string
    {
        $language = \Languages::DetectLanguage($language);
        $_3a9ee8592d2799f4e3ba31426430d858 = $this->isLanguageSupported($language);
        return $_3a9ee8592d2799f4e3ba31426430d858 ? $language : $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_4298577e41" . "894bcce23f8" . "96cd365aea4"];
    }
    
    
    
    
    
    
    
    public function isLanguageSupported(string $language) : bool
    {
        return $this->adapter->isLanguageSupported($language);
    }
    
    
    
    
    
    
    
    
    public function getInformation() : AiInformation
    {
        return $this->adapter->getInformation();
    }
    
    
    
    
    
    
    
    
    protected function dispatchEvent(string $_18c3ae9a896c5ec6d2e49d0d9d3852f2, $_9961f986823b4be7f5c4cc8379c6b228) : void
    {
        $event = new AiEvent($_9961f986823b4be7f5c4cc8379c6b228);
        $this->dispatcher->dispatch($event, $_18c3ae9a896c5ec6d2e49d0d9d3852f2);
    }
}