<?php

declare(strict_types=1);

namespace External\PompoBundle\EventSubscriber\Ordering;

use KupShop\OrderingBundle\Event\OrderItemInfoEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderItemInfoSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            OrderItemInfoEvent::INFO => [
                ['setOrderItemInfo', 200],
            ],
        ];
    }

    public function setOrderItemInfo(OrderItemInfoEvent $event): void
    {
        if (!($order = $event->getOrder())) {
            return;
        }

        if (!($item = ($order->fetchItems()[$event->getOrderItem()['id']] ?? null))) {
            return;
        }

        if (empty($item['note']['variationDescr'])) {
            return;
        }

        $event->addInfoLine('selectedVariation', $item['note']['variationDescr']);
    }
}
