<?php

namespace Storeplugin\PDFInvoice\Framework\Fields\Types;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Framework\Abstracts\AbstractField;

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

    /**
     * Render the field.
     *
     * @param array  $field
     * @param mixed  $value
     * @param string $prefix
     *
     * @return string
     */
    public function render(array $field, $value, string $prefix): string
    {
        $value = is_string($value) && $value !== '' ? $value : '#7c3aed';
        $isDisabled = $this->hasTruthyFlag($field, 'disabled');
        $input_class = $this->resolveClass(
            $field,
            'nxtf-color-input'
        );

        $attrs = $this->buildAttributes([
            'type'             => 'color',
            'id'               => $this->getFieldId($field),
            'name'             => $this->getFieldName($field['id'], $prefix),
            'value'            => $value,
            'class'            => $input_class,
            'disabled'         => $isDisabled ? true : null,
            'data-color-input' => '1',
        ]);

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