<?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) 2016-2018 Plumrocket Inc. (http://www.plumrocket.com)
 * @license     http://wiki.plumrocket.net/wiki/EULA  End-user License Agreement
 */

namespace Plumrocket\ProductFilter\Helper;

use Magento\Catalog\Model\Layer\FilterList;
use Plumrocket\ProductFilter\Model\Config\Source\AdditionalFilters;
use Plumrocket\ProductFilter\Model\Config\Source\Inputs;

class CustomOption extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Plumrocket\ProductFilter\Helper\Data
     */
    private $dataHelper;

    /**
     * @var array
     */
    private $customOptions;

    /**
     * CustomOption constructor.
     *
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Plumrocket\ProductFilter\Helper\Data $dataHelper
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Plumrocket\ProductFilter\Helper\Data $dataHelper
    ) {
        parent::__construct($context);
        $this->dataHelper = $dataHelper;
    }

    /**
     * @param string $code
     * @return bool
     */
    public function isCustomOption($code)
    {
        $aditionalCode = str_replace('_', ' ', $code);

        return in_array($code, $this->getEnabledCustomOptions(), true)
            || in_array($aditionalCode, $this->getEnabledCustomOptions(), true);
    }

    /**
     * @return array
     */
    public function getEnabledCustomOptions()
    {
        if (null === $this->customOptions) {
            $this->customOptions = [];
            foreach ($this->dataHelper->getSelectedCustomOptions() as $sel) {
                $this->customOptions[] = $sel;
            }
        }

        return $this->customOptions;
    }
}
