<?php

declare(strict_types=1);

/*
 * CoreShop
 *
 * This source file is available under the terms of the
 * CoreShop Commercial License (CCL)
 * Full copyright and license information is available in
 * LICENSE.md which is distributed with this source code.
 *
 * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.com)
 * @license    CoreShop Commercial License (CCL)
 *
 */

namespace CoreShop\Bundle\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;

class RequestResetPasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $identifier = $options['reset_identifier'];
        $typeClass = $identifier === 'email' ? EmailType::class : TextType::class;

        $builder->add($identifier, $typeClass, [
            'label' => sprintf('coreshop.form.customer.%s', $identifier),
            'constraints' => [
                new NotBlank(),
            ],
        ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefault('reset_identifier', 'email');
        $resolver->setAllowedTypes('reset_identifier', 'string');
        $resolver->setAllowedValues('reset_identifier', ['email', 'username']);
    }

    public function getBlockPrefix(): string
    {
        return 'coreshop_request_reset_password';
    }
}
