<?php

declare(strict_types=1);

namespace App\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\BookStackSettings;
use App\Repository\BookStackConfigurationRepository;

final readonly class BookStackSettingsProvider implements ProviderInterface
{
    public function __construct(
        private BookStackConfigurationRepository $configRepository,
    ) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): BookStackSettings
    {
        $config = $this->configRepository->findSingleton();
        $dto    = new BookStackSettings();

        if (null !== $config) {
            $dto->url      = $config->getUrl();
            $dto->hasToken = $config->hasToken();
        }

        return $dto;
    }
}
