<?php
declare(strict_types=1);

namespace Autogedal\Manufacturers\Block\Widget;

use Autogedal\Manufacturers\Model\Config\Source\Manufacturers;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Widget\Block\BlockInterface;

class ManufacturerInfo extends Template implements BlockInterface
{
    protected $_template = 'Autogedal_Manufacturers::widget/manufacturer_info.phtml';

    public function __construct(
        Context $context,
        private readonly Manufacturers $manufacturersSource,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    public function getSections(): array
    {
        $sections = [
            [
                'title' => __('Who is the manufacturer %1?', $this->getManufacturerName()),
                'text' => $this->getText('about_text')
            ],
            [
                'title' => __('Why should I trust this manufacturer'),
                'text' => $this->getText('trust_text')
            ]
        ];

        return array_filter($sections, static fn (array $section): bool => $section['text'] !== '');
    }

    private function getManufacturerName(): string
    {
        $labels = array_column($this->manufacturersSource->toOptionArray(), 'label', 'value');

        return (string)($labels[(string)$this->getData('attribute_value')] ?? '');
    }

    private function getText(string $key): string
    {
        return trim((string)$this->getData($key));
    }
}
