<?php
declare(strict_types=1);

namespace Autogedal\Manufacturers\Model\Config\Source;

use Magento\Cms\Model\ResourceModel\Page\CollectionFactory;
use Magento\Framework\Data\OptionSourceInterface;

class CmsPages implements OptionSourceInterface
{
    public function __construct(
        private readonly CollectionFactory $collectionFactory
    ) {
    }

    public function toOptionArray(): array
    {
        $collection = $this->collectionFactory->create();
        $collection->addFieldToSelect(['identifier', 'title']);
        $collection->setOrder('title', 'ASC');

        $options = [];
        foreach ($collection as $page) {
            $identifier = trim((string)$page->getIdentifier());
            if ($identifier === '') {
                continue;
            }

            $options[] = [
                'value' => $identifier,
                'label' => sprintf('%s [%s]', (string)$page->getTitle(), $identifier)
            ];
        }

        return $options;
    }
}
