<?php

namespace IMATHUZH\Qfq\Core\Renderer\FormElement\Base;

use IMATHUZH\Qfq\Core\Form\FormElement\AbstractFormElement;
use IMATHUZH\Qfq\Core\Helper\Support;
use IMATHUZH\Qfq\Core\Renderer\FormElement\NativeRenderer;

/**
 * JsonRenderer - renders a textarea for JSON configuration editing.
 *
 * The textarea has class "qfq-icj" and optionally a data-template attribute.
 * Client-side JavaScript transforms this into a configuration editor.
 */
class InputCollectionJsonRenderer extends NativeRenderer {

    /**
     * Renders the JSON textarea element.
     *
     * @param AbstractFormElement $fe
     * @return string
     */
    public function renderInput(AbstractFormElement $fe): string {
        $classes = 'form-control ' . implode(' ', $fe->cssClasses);
        $fe->htmlAttributes[CSS_CLASS] = $classes;

        $htmlAttributes = Support::arrayToXMLAttributes($fe->htmlAttributes);

        // Render textarea with JSON content
        return '<textarea ' . $htmlAttributes . '>' . $fe->value . '</textarea>';
    }
}