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

namespace Plumrocket\ProductFilter\Model\Config\Source;

class Inputs extends AbstractSource
{
    /**
     * @var \Plumrocket\ProductFilter\Model\FilterList
     */
    private $filterList;

    /**
     * Attributes excludes from attribute search list
     *
     * @var array
     */
    private $disallowedAttributes = ['color', 'price', 'rating'];

    /**
     * Inputs constructor.
     *
     * @param \Plumrocket\ProductFilter\Model\FilterList $filterList
     */
    public function __construct(\Plumrocket\ProductFilter\Model\FilterList $filterList)
    {
        $this->filterList = $filterList;
    }

    /**
     * Options getter
     *
     * @return array
     */
    public function toOptionArray()
    {
        $attributeList = $this->filterList->getAttributeList();

        $attributeList = $this->filterAttributeList($attributeList);
        $attributeList = $this->formatAttributeList($attributeList);
        return $attributeList;
    }

    /**
     * @return string[]
     */
    public function getDisallowedAttributes()
    {
        return $this->disallowedAttributes;
    }

    /**
     * @param array $attributeList
     * @return array
     */
    public function formatAttributeList(array $attributeList)
    {
        $formattedList = [];
        foreach ($attributeList as $value => $label) {
            $formattedList[] = ['value' => $value, 'label' => $label];
        }

        return $formattedList;
    }

    /**
     * @param $attributeList
     * @return array
     */
    public function filterAttributeList($attributeList)
    {
        $filter = function ($attributeCode) {
            return !in_array($attributeCode, $this->getDisallowedAttributes());
        };

        return array_filter($attributeList, $filter, ARRAY_FILTER_USE_KEY);
    }
}
