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

$GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"] = (@$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] ?: array()) + array("_552f2d07bd7a454fad6" . "c8688199c6578" => base64_decode("cHJvbXB0RmVlZ" . "GJhY2s="), "_3f7234c" . "ceea2d3d" . "417abd8c" . "14a5c5be" . "d" => base64_decode("Y2FuZ" . "GlkYX" . "Rlcw=" . "="), "_5c535722540e1ef86eeede2274ec9" . "cd4" => base64_decode("O" . "j" . "o" . "="), "_40376698c11f06b3791" . "6ac32836f9e67" => base64_decode("IHF1aWNrIGFjY2Vzc29yIG9ubHkgd29ya3Mgd2l0aCBhIHNpbmdsZSBjY" . "W5kaWRhdGUu"), "_31767d7a" . "d58e706db" . "f47b7a51a" . "0fe6c4" => base64_decode("Y291bnQ" . "="), "_40975a2619cd7e477b1" . "db9ef3df0f65b" => base64_decode("VG" . "hl" . "IA" . "=="), "_38f12d8" . "b8c719a8" . "ce0fc2fa" . "efc225f3" . "5" => base64_decode("YXJ" . "yYX" . "lfb" . "WFw"), "_44f07c2d93758d0ad74ff42656acdb" . "e5" => base64_decode("YmxvY2tSZWF" . "zb24="), "_193e79bad68" . "eb71078a6117" . "b54b075d6" => base64_decode("UmVjaWV2ZWQgbXVsdGlwbGUgY2FuZGlhd" . "GVzLg=="), "_317ddd45a8818509eb560" . "d6886c42a60" => base64_decode("Z" . "W" . "5" . "k"), "_4d707fab93bebb00" . "bbe69e8159697c90" => base64_decode(""));
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["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_38f12d8b8c719a8ce0fc2" . "faefc225f35"](function ($_4f91306e224d46b1fb1e0598b3442db6) {
            return Candidate::fromArray($_4f91306e224d46b1fb1e0598b3442db6);
        }, $_fd62bc4eaaf7d58dea9bada3e30443b9[$GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_3f7234cceea2d3d417" . "abd8c14a5c5bed"]] ?? []);
        return new self($_3f7234cceea2d3d417abd8c14a5c5bed, $_fd62bc4eaaf7d58dea9bada3e30443b9[$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_552f2d07bd7a454fad" . "6c8688199c6578"]][$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_44f07c2d93758d" . "0ad74ff42656acd" . "be5"]] ?? null);
    }
    public function getParts() : array
    {
        $_9961f986823b4be7f5c4cc8379c6b228 = [];
        foreach ($this->candidates as $_4f91306e224d46b1fb1e0598b3442db6) {
            $_9961f986823b4be7f5c4cc8379c6b228[] = $_4f91306e224d46b1fb1e0598b3442db6->getParts();
        }
        return $_9961f986823b4be7f5c4cc8379c6b228;
    }
    public function hasTerminated() : bool
    {
        
        
        $_4f91306e224d46b1fb1e0598b3442db6 = $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_317ddd45a8818509eb560d6886c42a" . "60"]($this->candidates);
        if (!empty($_4f91306e224d46b1fb1e0598b3442db6)) {
            return $_4f91306e224d46b1fb1e0598b3442db6->hasTerminated();
        }
        return false;
    }
    
    
    
    
    
    
    
    
    public function getFirstParts() : array
    {
        if ($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_31767d7ad58e706dbf47b7a51a0fe" . "6c4"]($this->candidates) > 1) {
            throw new ValueError($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_40975a2619cd7e477" . "b1db9ef3df0f65b"] . __CLASS__ . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_5c535722540e1ef86eeede227" . "4ec9cd4"] . __FUNCTION__ . $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_40376698c11f" . "06b37916ac328" . "36f9e67"] . $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_193e79bad" . "68eb71078a" . "6117b54b07" . "5d6"]);
        }
        return $this->candidates[0]->getParts();
    }
    
    
    
    
    
    
    
    
    public function getGeneratedText() : string
    {
        $_9961f986823b4be7f5c4cc8379c6b228 = $this->getFirstParts();
        if (empty($_9961f986823b4be7f5c4cc8379c6b228) || $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_31767d7ad" . "58e706dbf4" . "7b7a51a0fe" . "6c4"]($_9961f986823b4be7f5c4cc8379c6b228) > 1 || !empty($_9961f986823b4be7f5c4cc8379c6b228) && !$_9961f986823b4be7f5c4cc8379c6b228[0] instanceof TextPart) {
            foreach ($_9961f986823b4be7f5c4cc8379c6b228 as $_d962cfd1735adb2dfba46f69c7f15cb8) {
                if ($_d962cfd1735adb2dfba46f69c7f15cb8 instanceof TextPart) {
                    return $_d962cfd1735adb2dfba46f69c7f15cb8->getValue();
                }
            }
            
            
            return $GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_4d707fab93bebb00bbe69e8159697c" . "90"];
        }
        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;
    }
}