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

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

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

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

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

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

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

        // EmbedOne
        if (isset($data['transportZoneConstraints']) || (! empty($this->class->fieldMappings['transportZoneConstraints']['nullable']) && array_key_exists('transportZoneConstraints', $data))) {
            $return = $data['transportZoneConstraints'];
            if ($return !== null) {
                $embeddedDocument = $return;

                if (! is_array($embeddedDocument)) {
                    throw HydratorException::associationTypeMismatch('App\Document\TransportZone', 'transportZoneConstraints', 'array', gettype($embeddedDocument));
                }
        
                $className = $this->dm->getClassNameForAssociation($this->class->fieldMappings['transportZoneConstraints'], $embeddedDocument);
                $embeddedMetadata = $this->dm->getClassMetadata($className);
                $return = $embeddedMetadata->newInstance();

                $this->dm->getUnitOfWork()->setParentAssociation($return, $this->class->fieldMappings['transportZoneConstraints'], $document, 'transportZoneConstraints');

                $embeddedData = $this->dm->getHydratorFactory()->hydrate($return, $embeddedDocument, $hints);
                $embeddedId = $embeddedMetadata->identifier && isset($embeddedData[$embeddedMetadata->identifier]) ? $embeddedData[$embeddedMetadata->identifier] : null;

                if (empty($hints[Query::HINT_READ_ONLY])) {
                    $this->dm->getUnitOfWork()->registerManaged($return, $embeddedId, $embeddedData);
                }
            }

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

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