<?php

namespace Solspace\Freeform\Attributes\Property;

use Solspace\Freeform\Freeform;
use Symfony\Component\Serializer\Annotation\Ignore;

/**
 * @template T
 */
abstract class Property
{
    #[Ignore] public ?TransformerInterface $transformer = null;
    #[Ignore] public ?ValueGeneratorInterface $valueGenerator = null;

    /** @var PropertyValidatorInterface[] */
    #[Ignore] public array $validators = [];

    /** @var Middleware[] */
    public array $middleware = [];

    /** @var VisibilityFilter[] */
    public array $visibilityFilters = [];
    public ?Delimiter $delimiter = null;

    public bool $visible = true;
    public bool $translatable = false;
    public string $edition = Edition::EXPRESS;

    /** @var Flag[] */
    public array $flags = [];
    public array $messages = [];

    public ?string $section = null;
    public bool $required = false;
    public ?string $handle = null;
    public ?string $type = null;

    /**
     * @param T $value
     */
    public function __construct(
        public ?string $label = null,
        public ?string $instructions = null,
        public ?int $order = null,
        public mixed $value = null,
        public ?string $placeholder = null,
        public ?int $width = null,
        public ?bool $disabled = null,
    ) {}

    #[Ignore]
    public function hasFlag(...$name): bool
    {
        foreach ($this->flags as $flag) {
            if (\in_array($flag->name, $name, true)) {
                return true;
            }
        }

        return false;
    }

    public function matchesEdition(): bool
    {
        return Freeform::getInstance()->edition()->isAtLeast($this->edition);
    }

    #[Ignore]
    public function hasMiddlewareInstalled(...$name): bool
    {
        foreach ($this->middleware as $middleware) {
            if (\in_array($middleware->name, $name, true)) {
                return true;
            }
        }

        return false;
    }
}
