<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\PersonBundle\Form;

use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Form\DataMapper\PersonIdentifiersDataMapper;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

final class PersonIdentifiersType extends AbstractType
{
    public function __construct(
        private readonly PersonIdentifierManagerInterface $identifierManager,
        private readonly PersonIdentifiersDataMapper $identifiersDataMapper,
        private readonly TranslatableStringHelperInterface $translatableStringHelper,
    ) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        foreach ($this->identifierManager->getWorkers() as $worker) {
            if (!$worker->getDefinition()->isEditableByUsers()) {
                continue;
            }

            $subBuilder = $builder->create(
                'identifier_'.$worker->getDefinition()->getId(),
                options: [
                    'compound' => true,
                    'label' => $this->translatableStringHelper->localize($worker->getDefinition()->getLabel()),
                ]
            );
            $worker->buildForm($subBuilder);
            $builder->add($subBuilder);
        }

        $builder->setDataMapper($this->identifiersDataMapper);
    }
}
