<?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 AppDocumentIncidentHydrator 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;
        }

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

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

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

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

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}