<?php

namespace NoahVet\Reef\Application\Query;

use NoahVet\Reef\Application\Normalizer\PostNormalizerInterface;

class GetOneQuery implements GetOneQueryInterface
{
    /**
     * @param array<int|string, mixed> $normalizationAttributes
     * @param class-string             $janeModelFQCN
     */
    public function __construct(
        private readonly object $object,
        private readonly array $normalizationAttributes,
        private readonly string $janeModelFQCN,
        private readonly ?string $locale = null,
        private readonly ?PostNormalizerInterface $postNormalizer = null,
    ) {
    }

    #[\Override]
    public function object(): object
    {
        return $this->object;
    }

    /**
     * @return array<int|string, mixed>
     */
    #[\Override]
    public function normalizationAttributes(): array
    {
        return $this->normalizationAttributes;
    }

    #[\Override]
    public function janeModelFQCN(): string
    {
        return $this->janeModelFQCN;
    }

    #[\Override]
    public function locale(): ?string
    {
        return $this->locale;
    }

    #[\Override]
    public function postNormalize(): ?\Closure
    {
        if (null === $this->postNormalizer) {
            return null;
        }
        $normalizer = $this->postNormalizer;

        return static function (array &$item, ?string $locale) use ($normalizer): void {
            $normalizer->normalize(data: $item, locale: $locale);
        };
    }
}
