<?php
declare(strict_types=1);

namespace FiloBlu\Rma\Plugin\Sales\Model\Order;

use FiloBlu\Rma\Helper\BundlesHelper;
use Magento\Bundle\Model\Product\Type;

/**
 * @class ItemIsDummy
 * @package FiloBlu\Rma\Plugin
 */
class ItemIsDummy
{

    /**
     * @var BundlesHelper
     */
    private $bundlesHelper;

    /**
     * @param BundlesHelper $bundlesHelper
     */
    public function __construct(
        BundlesHelper $bundlesHelper
    ) {
        $this->bundlesHelper = $bundlesHelper;
    }

    /**
     * @param $subject
     * @param $result
     * @param $shipment
     * @return false|mixed
     * @see \Magento\Sales\Model\Order\Item::isDummy()
     */
    public function afterIsDummy($subject, $result, $shipment = false)
    {
        if (!$shipment || !$this->bundlesHelper->isAllowShippedSeparatelyItemsMarkedAsShipped()) {
            return $result;
        }

        if ($subject->getParentItem() && $subject->getParentItem()->getProductType() === Type::TYPE_CODE) {
            return false;
        }

        return $result;
    }
}
