<?php

namespace App\Twig\Components;

use Aimeos\Map;
use App\User\Framework\Repository\PreferencesRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
final class Filter
{
    use DefaultActionTrait;

    public array $preferences = [];

    public array $userPreferences = [];

    public function __construct(
        private readonly PreferencesRepository $preferencesRepository,
        private readonly Security $security,
    ) {
        $this->preferences = Map::from($this->preferencesRepository->findEnabled())
            ->rekey(fn($element) => $element->getId())
            ->map(fn($element) => $element->getPreferenceOptions()->toArray())
            ->toArray();
        $this->userPreferences = Map::from($this->security->getUser()->getUserPreferenceValues())
            ->toArray();
    }
}
