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

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

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

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

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

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