<?php

namespace Storeplugin\PDFInvoice\Framework\Fields\Types;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Framework\Abstracts\AbstractField;

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

    /**
     * Render the field.
     *
     * @param array  $field
     * @param mixed  $value
     * @param string $prefix
     *
     * @return string
     */
    public function render(array $field, $value, string $prefix): string
    {
        $min = $field['min'] ?? 0;
        $max = $field['max'] ?? 100;
        $step = $field['step'] ?? 1;
        $value = $value !== '' ? $value : $min;
        $isDisabled = $this->hasTruthyFlag($field, 'disabled');

        $attrs = $this->buildAttributes([
            'type'             => 'range',
            'id'               => $this->getFieldId($field),
            'name'             => $this->getFieldName($field['id'], $prefix),
            'value'            => $value,
            'min'              => $min,
            'max'              => $max,
            'step'             => $step,
            'class'            => $this->resolveClass($field, 'nxtf-field-range'),
            'disabled'         => $isDisabled ? true : null,
            'data-range-input' => $field['id'],
        ]);

        return sprintf(
            '<div class="nxtf-field-range-wrapper" data-range-wrapper="1"><input %s><span class="nxtf-range-data-value" data-range-value="1">%s</span></div>',
            $attrs,
            \esc_html($value)
        );
    }
}
