<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Form\Source;

use function YOOtheme\app;
use YOOtheme\Builder;
use YOOtheme\Str;

class FormType
{
    public const TYPE_NAME = 'YooessentialsForm';

    public static function config(array $controls): array
    {
        /** @var Builder $builder */
        $builder = app(Builder::class);

        $fields = [];
        foreach ($controls as $control) {
            $type = $builder->types[$control['type']] ?? null;

            if (!$type || !$type->submittable) {
                continue;
            }

            $name = $control['props']['control_name'] ?? '';
            $label = $control['props']['control_label'] ?? '';
            $multiple = $control['props']['control_multiple'] ?? false;

            $isListOf = $type->container;
            $typeName = str_replace('yooessentials_form_', '', $type->name);

            if (($typeName === 'upload' || $typeName === 'select') && $multiple) {
                $isListOf = true;
            }

            $fields[Str::camelCase($control['name'])] = [
                'type' => $isListOf ? ['listOf' => 'String'] : 'String',
                'metadata' => [
                    'label' => $label ?: Str::titleCase($name),
                    'es_field_meta' => ['control' => $name],
                    'filters' => [
                        Str::contains($typeName, 'date') ? 'date' : null,
                        Str::contains($typeName, 'text') ? 'limit' : null,
                    ],
                ],
            ];
        }

        return [
            'fields' => $fields,
            'metadata' => [
                'type' => true,
                'label' => 'Form',
            ],
        ];
    }
}
