<?php

namespace Eadesigndev\NemoExpress\Model\Config\Source;

#[\AllowDynamicProperties]
class Keys implements \Magento\Framework\Data\OptionSourceInterface
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * Example constructor.
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
    {
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * @return array
     */
    public function getAllOptions()
    {
        $content = $this->scopeConfig->getValue('carriers/nemoexpress/api_keys');

        $values = [];
        $lines = explode("\n", $content);
        foreach ($lines as $line) {
            $line = trim($line);
            if (!empty($line)) {
                $values[] = [
                    'value' => $line,
                    'label' => $line
                ];
            }
        }

        return $values;
    }

    /**
     * @param string $value
     * @return string
     */
    public function getOptionText($value)
    {
        foreach ($this->getAllOptions() as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
    }

    public function toOptionArray()
    {
        return $this->getAllOptions();
    }
}