<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Ui\Component\Listing\Order\Column;

use \Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Aitoc\PreOrders\Helper\PreOrder as PreOrderHelper;

/**
 * Class IsPreOrder
 */
class IsPreOrder extends Column
{
    /**
     * @var PreOrderHelper
     */
    private $preOrderHelper;

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param PreOrderHelper $preOrderHelper
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        PreOrderHelper $preOrderHelper,
        array $components,
        array $data
    ) {
        $this->preOrderHelper = $preOrderHelper;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $orderId = $item['entity_id'];
                $value = $this->preOrderHelper->isOrderContainsPreOrderProducts($orderId) ? 'Yes' : 'No';
                $item[$this->getData('name')] = __($value);
            }
        }

        return $dataSource;
    }
}
