<?php

namespace Hydrators;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Hydrator\HydratorException;
use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface;
use Doctrine\ODM\MongoDB\Query\Query;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;

use function array_key_exists;
use function gettype;
use function is_array;

/**
 * THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE.
 */
class AppDocumentContainerHydrator implements HydratorInterface
{
    public function __construct(private DocumentManager $dm, private ClassMetadata $class) {}

    public function hydrate(object $document, array $data, array $hints = []): array
    {
        $hydratedData = [];

        // Field(type: "string")
        if (isset($data['reflexId']) || (! empty($this->class->fieldMappings['reflexId']['nullable']) && array_key_exists('reflexId', $data))) {
            $value = $data['reflexId'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['reflexId']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['reflexId']->setValue($document, $return);
            $hydratedData['reflexId'] = $return;
        }

        // Field(type: "string")
        if (isset($data['clientId']) || (! empty($this->class->fieldMappings['clientId']['nullable']) && array_key_exists('clientId', $data))) {
            $value = $data['clientId'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['clientId']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['clientId']->setValue($document, $return);
            $hydratedData['clientId'] = $return;
        }

        // Field(type: "stockTypeCollection")
        if (isset($data['stockTypes']) || (! empty($this->class->fieldMappings['stockTypes']['nullable']) && array_key_exists('stockTypes', $data))) {
            $value = $data['stockTypes'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['stockTypes']['type'];
                
            $type = \Doctrine\ODM\MongoDB\Types\Type::getType($typeIdentifier);
            $return = $type->convertToPHPValue($value);
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['stockTypes']->setValue($document, $return);
            $hydratedData['stockTypes'] = $return;
        }

        // Field(type: "bool")
        if (isset($data['alreadyExported']) || (! empty($this->class->fieldMappings['alreadyExported']['nullable']) && array_key_exists('alreadyExported', $data))) {
            $value = $data['alreadyExported'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['alreadyExported']['type'];
                $return = (bool) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['alreadyExported']->setValue($document, $return);
            $hydratedData['alreadyExported'] = $return;
        }

        // Field(type: "bool")
        if (isset($data['stock']) || (! empty($this->class->fieldMappings['stock']['nullable']) && array_key_exists('stock', $data))) {
            $value = $data['stock'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['stock']['type'];
                $return = (bool) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['stock']->setValue($document, $return);
            $hydratedData['stock'] = $return;
        }

        // Field(type: "bool")
        if (isset($data['transfer']) || (! empty($this->class->fieldMappings['transfer']['nullable']) && array_key_exists('transfer', $data))) {
            $value = $data['transfer'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['transfer']['type'];
                $return = (bool) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['transfer']->setValue($document, $return);
            $hydratedData['transfer'] = $return;
        }

        // Field(type: "bool")
        if (isset($data['exportVtl']) || (! empty($this->class->fieldMappings['exportVtl']['nullable']) && array_key_exists('exportVtl', $data))) {
            $value = $data['exportVtl'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['exportVtl']['type'];
                $return = (bool) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['exportVtl']->setValue($document, $return);
            $hydratedData['exportVtl'] = $return;
        }

        // Field(type: "bool")
        if (isset($data['regulation']) || (! empty($this->class->fieldMappings['regulation']['nullable']) && array_key_exists('regulation', $data))) {
            $value = $data['regulation'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['regulation']['type'];
                $return = (bool) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['regulation']->setValue($document, $return);
            $hydratedData['regulation'] = $return;
        }

        // Field(type: "string")
        if (isset($data['type']) || (! empty($this->class->fieldMappings['type']['nullable']) && array_key_exists('type', $data))) {
            $value = $data['type'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['type']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['type']->setValue($document, $return);
            $hydratedData['type'] = $return;
        }

        // Field(type: "string")
        if (isset($data['reference']) || (! empty($this->class->fieldMappings['reference']['nullable']) && array_key_exists('reference', $data))) {
            $value = $data['reference'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['reference']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['reference']->setValue($document, $return);
            $hydratedData['reference'] = $return;
        }

        // Field(type: "date")
        if (array_key_exists('hourArrival', $data) && ($data['hourArrival'] !== null || ($this->class->fieldMappings['hourArrival']['nullable'] ?? false))) {
            $value = $data['hourArrival'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['hourArrival']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['hourArrival'] = $return;
        }

        // Field(type: "date")
        if (array_key_exists('etaw', $data) && ($data['etaw'] !== null || ($this->class->fieldMappings['etaw']['nullable'] ?? false))) {
            $value = $data['etaw'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['etaw']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['etaw'] = $return;
        }

        // Field(type: "string")
        if (isset($data['status']) || (! empty($this->class->fieldMappings['status']['nullable']) && array_key_exists('status', $data))) {
            $value = $data['status'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['status']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['status']->setValue($document, $return);
            $hydratedData['status'] = $return;
        }

        // Field(type: "string")
        if (isset($data['exportMode']) || (! empty($this->class->fieldMappings['exportMode']['nullable']) && array_key_exists('exportMode', $data))) {
            $value = $data['exportMode'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['exportMode']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['exportMode']->setValue($document, $return);
            $hydratedData['exportMode'] = $return;
        }

        // Field(type: "string")
        if (isset($data['warehouse']) || (! empty($this->class->fieldMappings['warehouse']['nullable']) && array_key_exists('warehouse', $data))) {
            $value = $data['warehouse'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['warehouse']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['warehouse']->setValue($document, $return);
            $hydratedData['warehouse'] = $return;
        }

        // EmbedMany
        $mongoData = $data['containerLines'] ?? null;

        if ($mongoData !== null && ! is_array($mongoData)) {
            throw HydratorException::associationTypeMismatch('App\Document\Container', 'containerLines', 'array', gettype($mongoData));
        }

        $return = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $this->class->fieldMappings['containerLines']);
        $return->setHints($hints);
        $return->setOwner($document, $this->class->fieldMappings['containerLines']);
        $return->setInitialized(false);
        if ($mongoData) {
            $return->setMongoData($mongoData);
        }
        $this->class->propertyAccessors['containerLines']->setValue($document, $return);
        $hydratedData['containerLines'] = $return;

        // EmbedMany
        $mongoData = $data['containerLinesUnloaded'] ?? null;

        if ($mongoData !== null && ! is_array($mongoData)) {
            throw HydratorException::associationTypeMismatch('App\Document\Container', 'containerLinesUnloaded', 'array', gettype($mongoData));
        }

        $return = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $this->class->fieldMappings['containerLinesUnloaded']);
        $return->setHints($hints);
        $return->setOwner($document, $this->class->fieldMappings['containerLinesUnloaded']);
        $return->setInitialized(false);
        if ($mongoData) {
            $return->setMongoData($mongoData);
        }
        $this->class->propertyAccessors['containerLinesUnloaded']->setValue($document, $return);
        $hydratedData['containerLinesUnloaded'] = $return;

        // ReferenceOne
        if (isset($data['organization']) || (! empty($this->class->fieldMappings['organization']['nullable']) && array_key_exists('organization', $data))) {
            $return = $data['organization'];
            if ($return !== null) {
                if ($this->class->fieldMappings['organization']['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID && ! is_array($return)) {
                    throw HydratorException::associationTypeMismatch('App\Document\Container', 'organization', 'array', gettype($return));
                }

                $className = $this->dm->getClassNameForAssociation($this->class->fieldMappings['organization'], $return);
                $identifier = ClassMetadata::getReferenceId($return, $this->class->fieldMappings['organization']['storeAs']);
                $targetMetadata = $this->dm->getClassMetadata($className);
                $id = $targetMetadata->getPHPIdentifierValue($identifier);
                $return = $this->dm->getReference($className, $id);
            }

            $this->class->propertyAccessors['organization']->setValue($document, $return);
            $hydratedData['organization'] = $return;
        }

        // Field(type: "id")
        if (isset($data['_id']) || (! empty($this->class->fieldMappings['id']['nullable']) && array_key_exists('_id', $data))) {
            $value = $data['_id'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['id']['type'];
                $return = $value instanceof \MongoDB\BSON\ObjectId ? (string) $value : $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['id']->setValue($document, $return);
            $hydratedData['id'] = $return;
        }

        // Field(type: "date")
        if (array_key_exists('createdAt', $data) && ($data['createdAt'] !== null || ($this->class->fieldMappings['createdAt']['nullable'] ?? false))) {
            $value = $data['createdAt'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['createdAt']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['createdAt'] = $return;
        }

        // Field(type: "date")
        if (array_key_exists('updatedAt', $data) && ($data['updatedAt'] !== null || ($this->class->fieldMappings['updatedAt']['nullable'] ?? false))) {
            $value = $data['updatedAt'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['updatedAt']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['updatedAt'] = $return;
        }

        return $hydratedData;
    }
}