<?php

namespace Storeplugin\PDFInvoice\Framework\Fields\Types;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Framework\Abstracts\AbstractField;

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

    /**
     * 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([
            'type'        => 'tel',
            'id'          => $this->getFieldId($field),
            'name'        => $this->getFieldName($field['id'], $prefix),
            'value'       => (string) $value,
            'class'       => $this->resolveClass($field, $this->getInputClasses()),
            'placeholder' => $field['placeholder'],
            'disabled'    => $isDisabled ? true : null,
            'readonly'    => $isReadonly ? true : null,
            'required'    => $isRequired ? true : null,
        ]);

        return sprintf('<input %s>', $attrs);
    }
}
