<?php

namespace KupShop\CatalogBundle\View;

use KupShop\CatalogBundle\Repository\ProducersRepository;
use KupShop\ComponentsBundle\View\ComponentsViewInterface;
use KupShop\KupShopBundle\Views\View;
use Symfony\Contracts\Service\Attribute\Required;

class ProducersListView extends View implements ComponentsViewInterface
{
    protected $template = 'producers.tpl';
    protected string $entrypoint = 'producers';

    #[Required]
    public ProducersRepository $producersRepository;

    public function __construct()
    {
        $this->proxyCacheEnabled = findModule(\Modules::PROXY_CACHE, 'content');
    }

    public function getBodyVariables(): array
    {
        $vars = parent::getBodyVariables();
        $vars['producers'] = $this->producersRepository->getProducers();

        return $vars;
    }

    public function fetchPhotos($size = 0): array
    {
        $this->producersRepository->setImageSize($size);

        return $this->producersRepository->getProducers(true);
    }

    public function getTitle()
    {
        return translate('returnNav', 'producers');
    }

    public function getWpjToolbar(): array
    {
        $arr = [
            'url' => getAdminUrl('list', ['type' => 'producers']),
            'title' => 'Upravit výrobce',
            'translateUrl' => getAdminUrl('list', [
                'type' => 'translateProducers',
                'iframe' => 1,
            ]),
            'translateTitle' => 'Překlad výrobců',
        ];

        return array_merge(parent::getWpjToolbar(), $arr);
    }

    /**
     * @return ProducersListView
     */
    public function setSorting($sorting)
    {
        $this->producersRepository->setSorting($sorting);

        return $this;
    }

    public function getTemplates(): iterable
    {
        yield $this->template;
    }
}
