<?php

declare(strict_types=1);

namespace Autogedal\AwbRetry\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
class Actions extends Column
{
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        private readonly UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                if (empty($item['entity_id'])) {
                    continue;
                }

                $name = $this->getData('name');
                $item[$name]['retry'] = [
                    'href' => $this->urlBuilder->getUrl('awb_retry/error/retry', ['id' => $item['entity_id']]),
                    'label' => __('Manual Retry'),
                ];
            }
        }

        return $dataSource;
    }
}
