<?php

namespace Solspace\Freeform\Fields\Implementations\Pro;

use GraphQL\Type\Definition\Type as GQLType;
use Solspace\Freeform\Attributes\Field\Type;
use Solspace\Freeform\Attributes\Property\Input;
use Solspace\Freeform\Attributes\Property\Translatable;
use Solspace\Freeform\Fields\Implementations\TextField;
use Solspace\Freeform\Fields\Interfaces\ExtraFieldInterface;
use Solspace\Freeform\Fields\Interfaces\SkipGibberishCheckInterface;

#[Type(
    name: 'Regex',
    typeShorthand: 'regex',
    iconPath: __DIR__.'/../Icons/regex.svg',
    previewTemplatePath: __DIR__.'/../PreviewTemplates/text.ejs',
)]
class RegexField extends TextField implements ExtraFieldInterface, SkipGibberishCheckInterface
{
    #[Translatable]
    #[Input\Text(
        instructions: 'Enter any regex pattern here.',
    )]
    protected string $pattern = '';

    #[Translatable]
    #[Input\TextArea(
        label: 'Error Message',
        instructions: 'The message a user should receive if an incorrect value is given. It will replace any occurrences of `{{pattern}}` with the supplied regex pattern inside the message if any are found.',
        value: 'Value is not valid',
    )]
    protected string $message = '';

    /**
     * Return the field TYPE.
     */
    public function getType(): string
    {
        return self::TYPE_REGEX;
    }

    public function getPattern(): string
    {
        return $this->getTranslationTable()->get('pattern', $this->pattern);
    }

    public function getMessage(): string
    {
        return $this->getTranslationTable()->get('message', $this->message ?: 'Value is not valid');
    }

    public function getContentGqlMutationArgumentType(): array|GQLType
    {
        $description = $this->getContentGqlDescription();

        if (!empty($this->getPattern())) {
            $description[] = 'Regex pattern: "'.$this->getPattern().'".';
        }

        $description = implode("\n", $description);

        return [
            'name' => $this->getContentGqlHandle(),
            'type' => $this->getContentGqlType(),
            'description' => trim($description),
        ];
    }
}
