<?php

namespace App\Twig\Extension;

use App\Dto\SnipFilterRequest;
use App\Repository\TagRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class SnipFilterExtension extends AbstractExtension
{
    public function __construct(
        private readonly TagRepository $tagRepository,
        private readonly Security      $security,
    ) {}

    public function getFunctions(): array
    {
        return [
            new TwigFunction('snipSortOptions', fn() => [
                SnipFilterRequest::SORT_NAME,
                SnipFilterRequest::SORT_DATE,
            ]),
            new TwigFunction('snipFilterOptions', fn() => [
                SnipFilterRequest::VISIBILITY_ALL,
                SnipFilterRequest::VISIBILITY_VISIBLE,
                SnipFilterRequest::VISIBILITY_HIDDEN,
                SnipFilterRequest::VISIBILITY_ARCHIVED,
            ]),
            new TwigFunction('snipTagOptions', fn() => $this->getSnipTagOptions()),
        ];
    }

    private function getSnipTagOptions(): array
    {
        $tags[SnipFilterRequest::TAG_ALL] = 'All tags';
        $tags[SnipFilterRequest::TAG_NONE] = 'No tags';
        foreach ($this->tagRepository->findAllByUser($this->security->getUser()) as $tag) {
            $tags[(string)$tag] = (string)$tag;
        }
        return $tags;
    }

}
