<?php

namespace Storeplugin\PDFInvoice\Framework\Fields\Types;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Framework\Abstracts\AbstractField;

class TextareaField extends AbstractField
{
    /**
     * Get the field type.
     *
     * @return string
     */
    public static function getType(): string
    {
        return 'textarea';
    }

    /**
     * Render the field.
     *
     * @param array  $field
     * @param mixed  $value
     * @param string $prefix
     *
     * @return string
     */
    public function render(array $field, $value, string $prefix): string
    {
        $isDisabled = $this->hasTruthyFlag($field, 'disabled');
        $isReadonly = $this->hasTruthyFlag($field, 'readonly');
        $isRequired = $this->hasTruthyFlag($field, 'required');

        $attrs = $this->buildAttributes([
            'id'          => $this->getFieldId($field),
            'name'        => $this->getFieldName($field['id'], $prefix),
            'rows'        => $field['rows'] ?? 4,
            'class'       => $this->resolveClass($field, 'nxtf-field-textarea'),
            'placeholder' => $field['placeholder'],
            'disabled'    => $isDisabled ? true : null,
            'readonly'    => $isReadonly ? true : null,
            'required'    => $isRequired ? true : null,
        ]);

        return sprintf('<textarea %s>%s</textarea>', $attrs, \esc_textarea((string) $value));
    }
}
