<?php
namespace Claromentis\Core\ORM\Entity;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"] = (@$GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"] ?: array()) + array("_db6f0ba0791e9a635407659a9fe04e" . "40" => base64_decode("aXNfb2JqZ" . "WN0"), "na" . "me" => base64_decode("bmFtZQ=" . "="), "_7f628626" . "dd1d55c71" . "b1fc0932f" . "fd1d2c" => base64_decode("aXN" . "fYX" . "JyY" . "Xk="), "_a46a5897eb03d952a1f1fba279cec" . "2fb" => base64_decode("VG9TdHJpbmc" . "="), "_cbc16e6843fb081a" . "429049d81663ce2b" => base64_decode("ZWxlbW" . "VudHM="), "_3079496ebde9e3f9f1ebd7031" . "287340f" => base64_decode("VG9BcnJheQ=" . "="), "_6edacb78bd55c5015af27c8ac1fc" . "d9af" => base64_decode("bWV0aG9kX2" . "V4aXN0cw=="));
use Analogue\ORM\Entity;
use Analogue\ORM\EntityCollection;
use Analogue\ORM\Mappable;
use Claromentis\Core\Http\HttpUtil;
use Claromentis\Core\Services;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use MetaDataSet;
use MetaDataSimple;
use MetaFieldFactory;
use MetaFieldPrototype;
use ProxyManager\Proxy\ProxyInterface;















class Metadata extends Entity
{
    
    
    
    
    
    
    protected $parent;
    
    
    
    
    
    
    protected $aggregation;
    
    
    
    
    
    
    protected $object_id;
    
    
    
    
    
    
    protected $set_id;
    
    
    
    
    
    
    protected $set;
    
    
    
    
    
    
    
    
    
    public function __construct(Mappable $_568fcb2872723a3ebac6afca64f9238e, int $aggregation, int $object_id, int $_8bd60f167b34bb2f633dd93934749b67 = 0)
    {
        $this->parent = $_568fcb2872723a3ebac6afca64f9238e;
        $this->aggregation = $aggregation;
        $this->object_id = $object_id;
        $this->set_id = $_8bd60f167b34bb2f633dd93934749b67;
        
        
        if ($this->set_id) {
            $this->set = MetaFieldFactory::I()->GetMetaDataSetCached($this->set_id);
        } else {
            $this->set = MetaFieldFactory::I()->GetSetForAggregation($this->aggregation);
        }
        $this->set_id = $this->set->set_id;
    }
    
    
    
    
    
    
    public function getAggregationAttribute()
    {
        return $this->aggregation;
    }
    
    
    
    
    
    
    public function getElementsAttribute()
    {
        return $this->attributes[$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_cbc16e6843fb081a429049d8" . "1663ce2b"]] ?? null;
    }
    
    
    
    
    
    
    public function setElementsAttribute(EntityCollection $_cbc16e6843fb081a429049d81663ce2b)
    {
        $this->attributes[$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["_cbc16e6843fb081a429049d8" . "1663ce2b"]] = $_cbc16e6843fb081a429049d81663ce2b;
    }
    
    
    
    
    
    
    public function getSetAttribute()
    {
        $this->set->LoadAllFields();
        return $this->set;
    }
    
    
    
    
    
    
    
    
    public function getElements()
    {
        $_cbc16e6843fb081a429049d81663ce2b = $this->attributes[$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_cbc16e6843fb081a4290" . "49d81663ce2b"]] ?? null;
        if (!$_cbc16e6843fb081a429049d81663ce2b) {
            return $_cbc16e6843fb081a429049d81663ce2b;
        }
        $_cd32cee6e5836ae05dc7b722f2ec21e9 = $this->set;
        
        
        
        
        
        
        return $_cbc16e6843fb081a429049d81663ce2b->groupBy($GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["nam" . "e"])->map(function ($value, $name) use($_cd32cee6e5836ae05dc7b722f2ec21e9) {
            $_13d133c615714d50832f54e91fea8fb0 = $_cd32cee6e5836ae05dc7b722f2ec21e9->GetMetaFieldByName($name);
            
            
            
            
            if ($_13d133c615714d50832f54e91fea8fb0 && !$_13d133c615714d50832f54e91fea8fb0->IsRepeatable()) {
                return $value->first();
            }
            return $value;
        });
    }
    
    
    
    
    
    
    public function setElements(Collection $_cbc16e6843fb081a429049d81663ce2b)
    {
        
        
        $this->attributes[$GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_cbc16e6843fb081a429049d81663ce" . "2b"]] = $_cbc16e6843fb081a429049d81663ce2b->flatten()->filter();
        
        
        $this->parent->metadata = $this->attributes[$GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_cbc16e6843f" . "b081a429049d" . "81663ce2b"]];
    }
    
    
    
    
    
    
    
    
    
    
    
    public function getElement($name)
    {
        if (!$this->set->GetMetaFieldByName($name)) {
            return null;
        }
        return $this->getElements()->get($name);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public function setElement($name, $_36d08e924097eb0ed4a0145266126380)
    {
        $_13d133c615714d50832f54e91fea8fb0 = $this->set->GetMetaFieldByName($name);
        if (!$_13d133c615714d50832f54e91fea8fb0) {
            return;
        }
        if ($_36d08e924097eb0ed4a0145266126380 instanceof Collection && !$_13d133c615714d50832f54e91fea8fb0->IsRepeatable()) {
            $_36d08e924097eb0ed4a0145266126380 = $_36d08e924097eb0ed4a0145266126380->first();
        }
        $_cbc16e6843fb081a429049d81663ce2b = $this->getElements();
        $_cbc16e6843fb081a429049d81663ce2b->put($name, $_36d08e924097eb0ed4a0145266126380);
        $this->setElements($_cbc16e6843fb081a429049d81663ce2b);
    }
    
    
    
    
    
    
    
    public function getField(string $name)
    {
        return $this->set->GetMetaFieldByName($name);
    }
    
    
    
    
    
    
    public function getFieldNames()
    {
        return $this->set->GetMetaFieldsNames();
    }
    
    
    
    
    
    
    
    
    
    public function getValue(string $name)
    {
        $_13d133c615714d50832f54e91fea8fb0 = $this->set->GetMetaFieldByName($name);
        $_36d08e924097eb0ed4a0145266126380 = $this->getElement($name);
        
        
        if (!$_13d133c615714d50832f54e91fea8fb0) {
            return null;
        }
        
        
        if ($_13d133c615714d50832f54e91fea8fb0->IsRepeatable()) {
            
            
            
            
            $_cbc16e6843fb081a429049d81663ce2b = $_36d08e924097eb0ed4a0145266126380;
            $value = [];
            
            
            if (!$_cbc16e6843fb081a429049d81663ce2b || !$_cbc16e6843fb081a429049d81663ce2b->count()) {
                return $value;
            }
            
            
            
            
            
            
            foreach ($_cbc16e6843fb081a429049d81663ce2b as $_36d08e924097eb0ed4a0145266126380) {
                $value[] = $_13d133c615714d50832f54e91fea8fb0->UseGetWrapper($_36d08e924097eb0ed4a0145266126380->getWrapperValues());
            }
            return $value;
        }
        
        
        if (!$_36d08e924097eb0ed4a0145266126380) {
            return null;
        }
        return $_13d133c615714d50832f54e91fea8fb0->UseGetWrapper($_36d08e924097eb0ed4a0145266126380->getWrapperValues());
    }
    
    
    
    
    
    
    
    
    
    public function setValue(string $name, $value)
    {
        $_13d133c615714d50832f54e91fea8fb0 = $this->set->GetMetaFieldByName($name);
        $_36d08e924097eb0ed4a0145266126380 = $this->getElement($name);
        
        
        if (!$_13d133c615714d50832f54e91fea8fb0) {
            return;
        }
        
        
        if ($value === null) {
            $this->setElement($name, $value);
            return;
        }
        
        
        if ($_13d133c615714d50832f54e91fea8fb0->IsRepeatable()) {
            
            
            
            
            
            $_cbc16e6843fb081a429049d81663ce2b = $_36d08e924097eb0ed4a0145266126380;
            $values = $value;
            
            
            $_e818946733d6737d8e2d71a2ae84d46b = new Collection();
            if ($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_7f628626dd1d55c71b1fc0932ffd1d2" . "c"]($values)) {
                foreach ($values as $_02ad4799244951c2d809ee37be9e79fd => $value) {
                    
                    
                    
                    $_36d08e924097eb0ed4a0145266126380 = $_cbc16e6843fb081a429049d81663ce2b && $_cbc16e6843fb081a429049d81663ce2b->has($_02ad4799244951c2d809ee37be9e79fd) ? $_cbc16e6843fb081a429049d81663ce2b->get($_02ad4799244951c2d809ee37be9e79fd) : $this->newElement($name);
                    $_36d08e924097eb0ed4a0145266126380->setWrapperValues($_13d133c615714d50832f54e91fea8fb0->UseSetWrapper($value));
                    $_e818946733d6737d8e2d71a2ae84d46b->put($_02ad4799244951c2d809ee37be9e79fd, $_36d08e924097eb0ed4a0145266126380);
                }
            }
            $this->setElement($name, $_e818946733d6737d8e2d71a2ae84d46b);
            return;
        }
        if (!$_36d08e924097eb0ed4a0145266126380) {
            $_36d08e924097eb0ed4a0145266126380 = $this->newElement($name);
        }
        $_36d08e924097eb0ed4a0145266126380->setWrapperValues($_13d133c615714d50832f54e91fea8fb0->UseSetWrapper($value));
        $this->setElement($name, $_36d08e924097eb0ed4a0145266126380);
    }
    
    
    
    
    
    
    public function setValues(array $values)
    {
        foreach ($values as $name => $value) {
            $this->setValue($name, $value);
        }
    }
    
    
    
    
    
    
    
    
    
    public function getDefaultValue(string $name)
    {
        $_13d133c615714d50832f54e91fea8fb0 = $this->set->GetMetaFieldByName($name);
        if (!$_13d133c615714d50832f54e91fea8fb0) {
            return null;
        }
        return $_13d133c615714d50832f54e91fea8fb0->GetDefaultValue();
    }
    
    
    
    
    
    
    
    public function newElement($name)
    {
        $_13d133c615714d50832f54e91fea8fb0 = $this->set->GetMetaFieldByName($name);
        $_36d08e924097eb0ed4a0145266126380 = new MetadataElement();
        $_36d08e924097eb0ed4a0145266126380->aggregation = $this->aggregation;
        $_36d08e924097eb0ed4a0145266126380->object_id = $this->object_id;
        $_36d08e924097eb0ed4a0145266126380->name = $name;
        $_36d08e924097eb0ed4a0145266126380->type = $_13d133c615714d50832f54e91fea8fb0 ? $_13d133c615714d50832f54e91fea8fb0->GetMetaType() : 0;
        $_36d08e924097eb0ed4a0145266126380->to_index = true;
        return $_36d08e924097eb0ed4a0145266126380;
    }
    
    
    
    
    
    
    
    
    
    
    public function getSimple($_96cf4aee6d96b799325194c7eb6365ef = false)
    {
        $_c0eb2ee5aeff988b31fcbb4cf26af87d = new \MetaDataSimple($this->aggregation, $this->object_id, $this->set_id);
        if ($_96cf4aee6d96b799325194c7eb6365ef) {
            $_c0eb2ee5aeff988b31fcbb4cf26af87d->Load();
        }
        return $_c0eb2ee5aeff988b31fcbb4cf26af87d;
    }
    
    
    
    
    
    
    
    
    
    public function __get($_c241f11ec2591cc626823468234ffaf4)
    {
        
        
        if ($this->set->GetMetaFieldByName($_c241f11ec2591cc626823468234ffaf4)) {
            return $this->getValue($_c241f11ec2591cc626823468234ffaf4);
        }
        
        
        return parent::__get($_c241f11ec2591cc626823468234ffaf4);
    }
    
    
    
    
    
    
    
    
    
    public function __set($_c241f11ec2591cc626823468234ffaf4, $value)
    {
        
        
        if ($this->set->GetMetaFieldByName($_c241f11ec2591cc626823468234ffaf4)) {
            $this->setValue($_c241f11ec2591cc626823468234ffaf4, $value);
            return;
        }
        
        
        parent::__set($_c241f11ec2591cc626823468234ffaf4, $value);
    }
    public function toArray()
    {
        
        
        if ($this->attributes[$GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_cbc16e6843f" . "b081a429049d" . "81663ce2b"]] instanceof ProxyInterface && !$this->attributes[$GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_cbc16e6843fb081a429049d81663ce" . "2b"]]->isProxyInitialized()) {
            return [];
        }
        $_147aa65e6f86b03cd87a88a580320c7e = $this->getFieldNames();
        $_fd62bc4eaaf7d58dea9bada3e30443b9 = [];
        
        
        
        
        
        foreach ($_147aa65e6f86b03cd87a88a580320c7e as $name) {
            $_13d133c615714d50832f54e91fea8fb0 = $this->getField($name);
            if (!$_13d133c615714d50832f54e91fea8fb0 || !$_13d133c615714d50832f54e91fea8fb0->IsVisible()) {
                continue;
            }
            $value = $this->getValue($name);
            if ($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["_db6f0ba07" . "91e9a63540" . "7659a9fe04" . "e40"]($value)) {
                if ($value instanceof \MetaDataFile) {
                    $value = Services::I()->{HttpUtil::class}->CreateInternalLink($value->GetLink());
                } elseif ($value instanceof \DateDay) {
                    $value = $value->getDate(DATE_FORMAT_DATE_DAY);
                } elseif ($value instanceof \Date) {
                    $value = $value->getDate(DATE_FORMAT_TIMESTAMP);
                } elseif ($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_6edacb78bd55c5015a" . "f27c8ac1fcd9af"]($value, $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_3079496ebde9e3f9f1ebd703" . "1287340f"])) {
                    $value = $value->ToArray();
                } elseif ($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_6edacb7" . "8bd55c50" . "15af27c8" . "ac1fcd9a" . "f"]($value, $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_a46a5897eb03d952a1f1fba279ce" . "c2fb"])) {
                    $value = $value->ToString();
                } else {
                    $value = null;
                }
            }
            $_fd62bc4eaaf7d58dea9bada3e30443b9[$name] = $value;
        }
        return $_fd62bc4eaaf7d58dea9bada3e30443b9;
    }
}