<?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 CreditNoteResponseBankAccountNormalizer 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\CreditNoteResponseBankAccount::class === $type;
    }

    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
    {
        return \is_object($data) && \NoahVet\Reef\Jane\Model\CreditNoteResponseBankAccount::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\CreditNoteResponseBankAccount();
        if (null === $data || false === \is_array($data)) {
            return $object;
        }
        if (\array_key_exists('bicSwift', $data) && null !== $data['bicSwift']) {
            $object->setBicSwift($data['bicSwift']);
            unset($data['bicSwift']);
        } elseif (\array_key_exists('bicSwift', $data) && null === $data['bicSwift']) {
            $object->setBicSwift(null);
        }
        if (\array_key_exists('filledMandateValidatedDate', $data) && null !== $data['filledMandateValidatedDate']) {
            $object->setFilledMandateValidatedDate($data['filledMandateValidatedDate']);
            unset($data['filledMandateValidatedDate']);
        } elseif (\array_key_exists('filledMandateValidatedDate', $data) && null === $data['filledMandateValidatedDate']) {
            $object->setFilledMandateValidatedDate(null);
        }
        if (\array_key_exists('iban', $data)) {
            $object->setIban($data['iban']);
            unset($data['iban']);
        }
        if (\array_key_exists('identityName', $data)) {
            $object->setIdentityName($data['identityName']);
            unset($data['identityName']);
        }
        if (\array_key_exists('rum', $data) && null !== $data['rum']) {
            $object->setRum($data['rum']);
            unset($data['rum']);
        } elseif (\array_key_exists('rum', $data) && null === $data['rum']) {
            $object->setRum(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['bicSwift'] = $data->getBicSwift();
        $dataArray['filledMandateValidatedDate'] = $data->getFilledMandateValidatedDate();
        $dataArray['iban'] = $data->getIban();
        $dataArray['identityName'] = $data->getIdentityName();
        $dataArray['rum'] = $data->getRum();
        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\CreditNoteResponseBankAccount::class => false];
    }
}
