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

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}