<?php
/**
 * Plumrocket Inc.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the End-user License Agreement
 * that is available through the world-wide-web at this URL:
 * http://wiki.plumrocket.net/wiki/EULA
 * If you are unable to obtain it through the world-wide-web, please
 * send an email to support@plumrocket.com so we can send you a copy immediately.
 *
 * @package     Plumrocket Product Filter v3.x.x
 * @copyright   Copyright (c) 2020 Plumrocket Inc. (http://www.plumrocket.com)
 * @license     http://wiki.plumrocket.net/wiki/EULA  End-user License Agreement
 */

namespace Plumrocket\ProductFilter\Plugin\Model\Catalog\Layer\Filter\DataProvider;

class Price
{
    /**
     * @var \Plumrocket\ProductFilter\Helper\Data
     */
    private $dataHelper;

    /**
     * @param \Plumrocket\ProductFilter\Helper\Data $dataHelper
     */
    public function __construct(
        \Plumrocket\ProductFilter\Helper\Data $dataHelper
    ) {
        $this->dataHelper = $dataHelper;
    }

    /**
     * Skip max price modification
     *
     * @param string $filter
     * @return array|bool
     */
    public function afterValidateFilter(\Magento\Catalog\Model\Layer\Filter\DataProvider\Price $subject, $result)
    {
        if (! $this->dataHelper->moduleEnabled() || count($result) != 2) {
            return $result;
        }

        if (! defined('Magento\CatalogSearch\Model\Layer\Filter\Price::PRICE_DELTA')) {
            return $result;
        }

        if (!empty($result[1])) {
            $result[1] += \Magento\CatalogSearch\Model\Layer\Filter\Price::PRICE_DELTA + .01;
        }

        return $result;
    }
}
