<?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;

use ZOOlanders\YOOessentials\Vendor\Respect\Validation\Validator;

$input = require __DIR__ . '/../form_input_text/element.php';

return array_merge($input, [
    'transforms' => [
        'render' => function (object $node, array $params) use ($input) {
            $input['transforms']['render']($node, $params);

            try {
                if ($value = $node->control->value ?? null) {
                    $value = new \DateTime($value);
                    $node->control->value = $value->format('Y-m');
                }
            } catch (\Exception $e) {
            }
        },
    ],

    'validation' => function ($control, Validator $validator) {
        $props = $control['props'];

        if ($min = $props['minmonth'] ?? false) {
            $validator->min($min);
        }

        if ($max = $props['maxmonth'] ?? false) {
            $validator->max($max);
        }

        if ($props['required'] ?? false) {
            $validator->notOptional();
        }

        return $validator;
    },
]);
