<?php

declare(strict_types=1);

namespace External\PompoBundle\Overrides\GraphQL\Types\Extension;

use External\PompoBundle\Util\Order\PompoOrderUtil;
use KupShop\GraphQLBundle\ApiAdmin\Types\Order\OrderItem;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
use TheCodingMachine\GraphQLite\Annotations\Field;

#[ExtendType(class: OrderItem::class)]
class OrderItemExtension
{
    #[Field(description: 'Kód položky.')]
    public function getCode(OrderItem $item): ?string
    {
        if ($chargeCode = $this->getPompoOrderUtil()->getOrderItemChargeCode($item->item)) {
            return $chargeCode;
        }

        return $item->getCode();
    }

    private function getPompoOrderUtil(): PompoOrderUtil
    {
        static $pompoOrderUtil;

        if (!$pompoOrderUtil) {
            $pompoOrderUtil = ServiceContainer::getService(PompoOrderUtil::class);
        }

        return $pompoOrderUtil;
    }
}
