<?php

declare(strict_types=1);

namespace Gls\GlsPoland\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;

final class BulkDeleteConsignmentsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('consignmentIds', CollectionType::class, [
                'allow_add' => true,
                'entry_type' => HiddenType::class,
                'label' => false,
            ])
            ->get('consignmentIds')
            ->addModelTransformer(new CallbackTransformer(
                static function ($consignmentIds) {
                    return $consignmentIds;
                },
                static function (array $consignmentIds) {
                    return array_values(array_filter(array_map(static function ($consignmentId) {
                        return (int) $consignmentId;
                    }, $consignmentIds)));
                }
            ));
    }
}
