<?php

namespace KupShop\ElninoBundle\EventListener;

use KupShop\OrderingBundle\Event\OrderItemEvent;
use KupShop\OrderingBundle\Util\Order\OrderItemInfo;
use Product;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderItemListener implements EventSubscriberInterface
{
    /** @required */
    public OrderItemInfo $orderItemInfo;

    public static function getSubscribedEvents()
    {
        return [OrderItemEvent::ITEM_CREATED => [
            ['assignCurrentActiveElninoStore', 200],
        ],
        ];
    }

    public function assignCurrentActiveElninoStore(OrderItemEvent $event)
    {
        // Detect not loaded product
        if ($event->getProduct()->visible == null) {
            $event->getProduct()->createFromDB();
        }

        $elnino_store = $event->getProduct()->getData()['elnino_store'] ?? null;

        if ($elnino_store) {
            $this->orderItemInfo->setCustomDataItem($event->getData()['row'], 'elnino_store', $elnino_store);
        }
    }
}
