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

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

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

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

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

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

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

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