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

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

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

        // 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: "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;
        }

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}