<?php

namespace KupShop\ComponentsBundle\Twig\Components\Product;

use KupShop\CatalogBundle\ProductList\ProductCollection;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\KupShopBundle\Util\Functional\Mapping;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Product/Charges/Charges.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Charges extends BaseComponent
{
    use ProductDataTrait;

    public ?string $title = null;

    protected array $charges;

    public function getCharges(): array
    {
        if (!isset($this->charges)) {
            $this->charges = $this->product->fetchCharges();
            if ($products = array_filter(array_column($this->charges, 'product'))) {
                $products = Mapping::mapKeys($products, fn ($k, $p) => [$p->id.(isset($p->variationId) ? "/{$p->variationId}" : ''), $p]);
                $collection = (new ProductCollection($products))->setEntityType(\FilterParams::ENTITY_VARIATION);
                $collection->fetchProductsPhotoIds();
            }
        }

        return $this->charges;
    }
}
