<?php

declare(strict_types=1);

namespace Autogedal\Extend\Model\Elasticsearch\Adapter\DataMapper;

use Amasty\Sorting\Helper\Data;
use Amasty\Sorting\Model\Elasticsearch\Adapter\DataMapperInterface;
use Amasty\Sorting\Model\Elasticsearch\SkuRegistry;
use Amasty\Sorting\Model\Inventory\GetQtyInterface;
use Amasty\Sorting\Model\ResourceModel\Inventory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;

class HighPriority implements DataMapperInterface
{
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product
     */
    protected $_resourceProduct;

    /**
     * @var Data
     */
    private $data;

    /**
     * @var Inventory
     */
    private $inventory;

    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var SkuRegistry
     */
    private $skuRegistry;

    /**
     * @var GetQtyInterface
     */
    private $getQty;

    public function __construct(
        Data $data,
        Inventory $inventory,
        StoreManagerInterface $storeManager,
        SkuRegistry $skuRegistry,
        GetQtyInterface $getQty,
        ProductResource $_resourceProduct
    ) {
        $this->_resourceProduct = $_resourceProduct;
        $this->data = $data;
        $this->inventory = $inventory;
        $this->storeManager = $storeManager;
        $this->skuRegistry = $skuRegistry;
        $this->getQty = $getQty;
    }

    public function map(int $entityId, array $entityIndexData, int $storeId, ?array $context = []): array
    {
        $sku = $this->skuRegistry->getSku((int) $entityId);

        if (!$sku) {
            return ['product_priority_high' => 0];
        }

        $priority = $this->_resourceProduct->getAttributeRawValue(
            $entityId,
            'product_priority_high',
            0
        );

        if (isset($priority) && $priority == 1) {
            $value = 1;
        } else {
            $value = 0;
        }

        return ['product_priority_high' => $value];
    }

    public function isAllowed(int $storeId): bool
    {
        return true;
    }
}
