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

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

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

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

                $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['administrativeAddress']->setValue($document, $return);
            $hydratedData['administrativeAddress'] = $return;
        }

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

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

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

                $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['taxOfficeAddress']->setValue($document, $return);
            $hydratedData['taxOfficeAddress'] = $return;
        }

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

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

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

                $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['exporterContactInfos']->setValue($document, $return);
            $hydratedData['exporterContactInfos'] = $return;
        }

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

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

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

                $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['importerContactInfos']->setValue($document, $return);
            $hydratedData['importerContactInfos'] = $return;
        }

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

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

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

        return $hydratedData;
    }
}