<?php
/**
 * Copyright © 2018 EaDesign by Eco Active S.R.L. All rights reserved.
 * See LICENSE for license details.
 */
namespace Eadesigndev\Urgent\Model\Sources;

use Magento\Framework\Data\OptionSourceInterface;

/**
 * Class AbstractSource
 * @package Eadesigndev\Urgent\Model\Source
 */
#[\AllowDynamicProperties]
abstract class AbstractSource implements OptionSourceInterface
{
    private $scopeConfig;
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    )
    {
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * Get options
     *
     * @return array
     */
    public function toOptionArray()
    {
        $options[] = ['label' => '', 'value' => ''];
        $availableOptions = $this->getAvailable();
        if (!empty($availableOptions)) {
            foreach ($availableOptions as $key => $value) {
                $options[$key] = [
                    'label' => $value,
                    'value' => $key,
                ];
            }
        }

        return $options;
    }

    public function configToValues($configPath)
    {
        $value = $this->scopeConfig->getValue($configPath);
        $values = [];

        foreach(explode(PHP_EOL, $value) as $item) {
            $itemValue = explode(",", $item);
            $values[$itemValue[0]] = $itemValue[1];
        }

        return $values;
    }
}
