<?php
namespace Claromentis\Core\Ai\Adapters\Vertex;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"] = (@$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"] ?: array()) + array("_40376698c11f06b3791" . "6ac32836f9e67" => base64_decode("IHF1aWNrIGFjY2Vzc29yIG9ubHkgd29ya3Mgd2l0aCBhIHNpbmds" . "ZSBjYW5kaWRhdGUu"), "_31767d7ad58e706dbf47b7a51a0fe6c" . "4" => base64_decode("Y291bnQ" . "="), "_5c53572254" . "0e1ef86eeed" . "e2274ec9cd4" => base64_decode("O" . "j" . "o" . "="), "_193e79bad68eb71078a6117b54b07" . "5d6" => base64_decode("UmVjaWV2ZWQgbXVsdGlwbGUgY2FuZGlh" . "dGVzLg=="), "_40975a2619cd7e" . "477b1db9ef3df0f" . "65b" => base64_decode("VGhlI" . "A=="), "_3f7234cceea2" . "d3d417abd8c14" . "a5c5bed" => base64_decode("Y2Fu" . "ZGlk" . "YXRl" . "cw=="), "_38f12d8b" . "8c719a8ce" . "0fc2faefc" . "225f35" => base64_decode("YXJyYXl" . "fbWFw"), "_317ddd45a8818509eb560d6886c4" . "2a60" => base64_decode("Z" . "W" . "5" . "k"), "_552f2d07bd7a454fad6c8688199c65" . "78" => base64_decode("cHJvbXB0RmV" . "lZGJhY2s="), "_4d707fab93bebb00bbe69e" . "8159697c90" => base64_decode(""), "_44f07c2" . "d93758d0" . "ad74ff42" . "656acdbe" . "5" => base64_decode("YmxvY2" . "tSZWFz" . "b24="));
use Claromentis\Core\Ai\Adapters\Vertex\Candidate;
use Claromentis\Core\Ai\AiResponseInterface;
use Claromentis\Core\Ai\Exceptions\AiContentFilterException;
use Claromentis\Core\Ai\Exceptions\AiInvalidResponseException;
use Claromentis\Core\Ai\Parts\FilePart;
use Claromentis\Core\Ai\Parts\TextPart;
use ValueError;
use Webmozart\Assert\Assert;






class GenerateContentResponse implements AiResponseInterface
{
    
    
    
    
    
    
    private array $candidates;
    
    
    
    
    
    
    private ?string $promptBlockReason;
    
    
    
    
    
    public function __construct(array $_3f7234cceea2d3d417abd8c14a5c5bed, ?string $_7c858ff752c623f4571ab25472751b4e = null)
    {
        Assert::allIsInstanceOf($_3f7234cceea2d3d417abd8c14a5c5bed, Candidate::class);
        $this->candidates = $_3f7234cceea2d3d417abd8c14a5c5bed;
        $this->promptBlockReason = $_7c858ff752c623f4571ab25472751b4e;
    }
    
    
    
    
    
    
    
    public static function fromArray(array $_fd62bc4eaaf7d58dea9bada3e30443b9) : self
    {
        $_3f7234cceea2d3d417abd8c14a5c5bed = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_38f12d8b8c719" . "a8ce0fc2faefc2" . "25f35"](function ($_4f91306e224d46b1fb1e0598b3442db6) {
            return Candidate::fromArray($_4f91306e224d46b1fb1e0598b3442db6);
        }, $_fd62bc4eaaf7d58dea9bada3e30443b9[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_3f7234cceea2d3d417ab" . "d8c14a5c5bed"]] ?? []);
        return new self($_3f7234cceea2d3d417abd8c14a5c5bed, $_fd62bc4eaaf7d58dea9bada3e30443b9[$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_552f2d07bd7a454fad" . "6c8688199c6578"]][$GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_44f07c2d93758d0a" . "d74ff42656acdbe5"]] ?? null);
    }
    public function getParts() : array
    {
        $_9961f986823b4be7f5c4cc8379c6b228 = [];
        foreach ($this->candidates as $_4f91306e224d46b1fb1e0598b3442db6) {
            $_9961f986823b4be7f5c4cc8379c6b228[] = $_4f91306e224d46b1fb1e0598b3442db6->getParts();
        }
        return $_9961f986823b4be7f5c4cc8379c6b228;
    }
    public function hasTerminated() : bool
    {
        
        
        $_4f91306e224d46b1fb1e0598b3442db6 = $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_317ddd45a8818509eb560d6886c42a" . "60"]($this->candidates);
        if (!empty($_4f91306e224d46b1fb1e0598b3442db6)) {
            return $_4f91306e224d46b1fb1e0598b3442db6->hasTerminated();
        }
        return false;
    }
    
    
    
    
    
    
    
    
    public function getFirstParts() : array
    {
        if ($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_31767d7ad58e706dbf47b7" . "a51a0fe6c4"]($this->candidates) > 1) {
            throw new ValueError($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["_40975a2619cd7e477b1db9ef3df" . "0f65b"] . __CLASS__ . $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_5c535722540e1ef86eeed" . "e2274ec9cd4"] . __FUNCTION__ . $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_40376698c11f06b37916ac328" . "36f9e67"] . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_193e79bad68eb71078a6117b54b0" . "75d6"]);
        }
        return $this->candidates[0]->getParts();
    }
    
    
    
    
    
    
    
    
    public function getGeneratedText() : string
    {
        $_9961f986823b4be7f5c4cc8379c6b228 = $this->getFirstParts();
        if (empty($_9961f986823b4be7f5c4cc8379c6b228) || $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_31767d7ad58e706dbf47" . "b7a51a0fe6c4"]($_9961f986823b4be7f5c4cc8379c6b228) > 1 || !empty($_9961f986823b4be7f5c4cc8379c6b228) && !$_9961f986823b4be7f5c4cc8379c6b228[0] instanceof TextPart) {
            foreach ($_9961f986823b4be7f5c4cc8379c6b228 as $_d962cfd1735adb2dfba46f69c7f15cb8) {
                if ($_d962cfd1735adb2dfba46f69c7f15cb8 instanceof TextPart) {
                    return $_d962cfd1735adb2dfba46f69c7f15cb8->getValue();
                }
            }
            
            
            return $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_4d707fab93bebb00bbe69e8" . "159697c90"];
        }
        return $_9961f986823b4be7f5c4cc8379c6b228[0]->getValue();
    }
    
    
    
    
    
    
    
    
    
    
    public function getGeneratedImages() : array
    {
        $_f083878fb61180fbf68ba3a77b8d7de3 = [];
        foreach ($this->candidates as $_4f91306e224d46b1fb1e0598b3442db6) {
            foreach ($_4f91306e224d46b1fb1e0598b3442db6->getParts() as $_d962cfd1735adb2dfba46f69c7f15cb8) {
                if ($_d962cfd1735adb2dfba46f69c7f15cb8 instanceof FilePart && $_d962cfd1735adb2dfba46f69c7f15cb8->isImage()) {
                    $_f083878fb61180fbf68ba3a77b8d7de3[] = $_d962cfd1735adb2dfba46f69c7f15cb8;
                }
            }
        }
        if (empty($_f083878fb61180fbf68ba3a77b8d7de3)) {
            if ($this->isFiltered()) {
                throw new AiContentFilterException();
            }
        }
        return $_f083878fb61180fbf68ba3a77b8d7de3;
    }
    
    
    
    
    
    
    
    
    private function isFiltered() : bool
    {
        if ($this->promptBlockReason !== null) {
            return true;
        }
        foreach ($this->candidates as $_4f91306e224d46b1fb1e0598b3442db6) {
            if ($_4f91306e224d46b1fb1e0598b3442db6->hasTerminated()) {
                return true;
            }
        }
        return false;
    }
}