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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}