<?php

namespace Lucent\Schema\BlockUi;

use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Validator\MinMaxInterface;

class Reference implements FieldInterface, MinMaxInterface
{
    public FieldInfo $info;

    /**
     * @param string[] $collections
     */
    public function __construct(
        public string $name,
        public string $label,
        public ?int   $min = null,
        public ?int   $max = null,
        public array  $collections = [],
        public string  $searchField = "",
        public string $layout = "",
    )
    {
        $this->info = new FieldInfo("reference", "Reference", FieldType::REFERENCE);
    }

    public function format(array $input, array $output): array
    {
        return $output;
    }

    public function failMin(mixed $value): bool
    {
        if (is_null($value)) {
            return false;
        }

        return count($value) < $this->min;
    }

    public function failMax(mixed $value): bool
    {
        if (is_null($value)) {
            return false;
        }

        return count($value) < $this->min;
    }

}

