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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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