<?php

namespace NoahVet\Reef\Jane\Normalizer;

use Jane\Component\JsonSchemaRuntime\Reference;
use NoahVet\Reef\Jane\Runtime\Normalizer\CheckArray;
use NoahVet\Reef\Jane\Runtime\Normalizer\ValidatorTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class V1ContactsIdPatchResponse200CompanyDetailNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
    use CheckArray;
    use DenormalizerAwareTrait;
    use NormalizerAwareTrait;
    use ValidatorTrait;

    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
    {
        return \NoahVet\Reef\Jane\Model\V1ContactsIdPatchResponse200CompanyDetail::class === $type;
    }

    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
    {
        return \is_object($data) && \NoahVet\Reef\Jane\Model\V1ContactsIdPatchResponse200CompanyDetail::class === $data::class;
    }

    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
    {
        if (isset($data['$ref'])) {
            return new Reference($data['$ref'], $context['document-origin']);
        }
        if (isset($data['$recursiveRef'])) {
            return new Reference($data['$recursiveRef'], $context['document-origin']);
        }
        $object = new \NoahVet\Reef\Jane\Model\V1ContactsIdPatchResponse200CompanyDetail();
        if (null === $data || false === \is_array($data)) {
            return $object;
        }
        if (\array_key_exists('legalCode', $data) && null !== $data['legalCode']) {
            $object->setLegalCode($data['legalCode']);
            unset($data['legalCode']);
        } elseif (\array_key_exists('legalCode', $data) && null === $data['legalCode']) {
            $object->setLegalCode(null);
        }
        if (\array_key_exists('name', $data) && null !== $data['name']) {
            $object->setName($data['name']);
            unset($data['name']);
        } elseif (\array_key_exists('name', $data) && null === $data['name']) {
            $object->setName(null);
        }
        if (\array_key_exists('serviceName', $data) && null !== $data['serviceName']) {
            $object->setServiceName($data['serviceName']);
            unset($data['serviceName']);
        } elseif (\array_key_exists('serviceName', $data) && null === $data['serviceName']) {
            $object->setServiceName(null);
        }
        if (\array_key_exists('type', $data) && null !== $data['type']) {
            $object->setType($data['type']);
            unset($data['type']);
        } elseif (\array_key_exists('type', $data) && null === $data['type']) {
            $object->setType(null);
        }
        if (\array_key_exists('vatIntraCommunityNumber', $data) && null !== $data['vatIntraCommunityNumber']) {
            $object->setVatIntraCommunityNumber($data['vatIntraCommunityNumber']);
            unset($data['vatIntraCommunityNumber']);
        } elseif (\array_key_exists('vatIntraCommunityNumber', $data) && null === $data['vatIntraCommunityNumber']) {
            $object->setVatIntraCommunityNumber(null);
        }
        foreach ($data as $key => $value) {
            if (\preg_match('/.*/', (string) $key)) {
                $object[$key] = $value;
            }
        }

        return $object;
    }

    public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
    {
        $dataArray = [];
        $dataArray['legalCode'] = $data->getLegalCode();
        $dataArray['name'] = $data->getName();
        $dataArray['serviceName'] = $data->getServiceName();
        $dataArray['type'] = $data->getType();
        $dataArray['vatIntraCommunityNumber'] = $data->getVatIntraCommunityNumber();
        foreach ($data as $key => $value) {
            if (\preg_match('/.*/', (string) $key)) {
                $dataArray[$key] = $value;
            }
        }

        return $dataArray;
    }

    public function getSupportedTypes(?string $format = null): array
    {
        return [\NoahVet\Reef\Jane\Model\V1ContactsIdPatchResponse200CompanyDetail::class => false];
    }
}
