<?php

namespace KupShop\CatalogBundle\Twig\Components\Producers;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\KupShopBundle\Context\LanguageContext;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Catalog/components/Producers/Listing/Listing.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Listing extends BaseComponent
{
    public array $producers = [];

    public function __construct(private readonly LanguageContext $languageContext)
    {
    }

    public function isNumeric($var): bool
    {
        return is_numeric($var);
    }

    public function getIsCZorSK(): bool
    {
        return in_array($this->languageContext->getActiveId(), ['cs', 'sk']);
    }

    public function getUrl(int $idProducer, string $producerTitle): string
    {
        return htmlspecialchars(createScriptURL([
            's' => 'category',
            'IDpd' => $idProducer,
            'producerTitle' => $producerTitle,
        ]), ENT_QUOTES);
    }

    public function getPhoto(array $producer): ?Thumbnail
    {
        return new Thumbnail((string) $producer['image']['id'], $producer['image']['descr'], (string) $producer['image']['date_update']);
    }

    public function getSortedProducers($producers): array
    {
        $collator = new \Collator('cs_CZ');
        usort($producers, function ($a, $b) use ($collator) {
            return $collator->compare($a['name'], $b['name']);
        });

        return $producers;
    }
}
