<?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 AppDocumentTransferHydrator implements HydratorInterface
{
    public function __construct(private DocumentManager $dm, private ClassMetadata $class) {}

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

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

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

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

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

        $return = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $this->class->fieldMappings['transferLines']);
        $return->setHints($hints);
        $return->setOwner($document, $this->class->fieldMappings['transferLines']);
        $return->setInitialized(false);
        if ($mongoData) {
            $return->setMongoData($mongoData);
        }
        $this->class->propertyAccessors['transferLines']->setValue($document, $return);
        $hydratedData['transferLines'] = $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['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: "bool")
        if (isset($data['shipped']) || (! empty($this->class->fieldMappings['shipped']['nullable']) && array_key_exists('shipped', $data))) {
            $value = $data['shipped'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['shipped']['type'];
                $return = (bool) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['shipped']->setValue($document, $return);
            $hydratedData['shipped'] = $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\Transfer', '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;
        }

        // ReferenceMany
        $mongoData = $data['containers'] ?? null;

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

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