{% import '@admin/configuration/macros.twig' as macros %}

{% for key, configItem in configurationList %}
  <div class="row mb-2 p-1">
    {% if configItem.element == 'area' %}
      {{ macros.generateTextarea(configItem.label, key, configurationData[key], configItem.description) }}
    {% elseif configItem.element == 'checkbox' %}
      {{ macros.generateCheckbox(configItem.label, key, configurationData, specialCases, configItem.description) }}
    {% elseif configItem.element == 'password' %}
      {{ macros.generatePasswordInput(configItem.label, key, configurationData[key]) }}
    {% elseif configItem.element == 'input' %}
      {{ macros.generateInputGroup(configItem.label, key, configurationData, configItem.description) }}
    {% elseif configItem.element == 'button' %}
      {{ macros.sendTestMailButton(configItem.label, key, configItem.description) }}
    {% elseif configItem.element == 'print' %}
      {{ macros.readOnlyTextInput(configItem.label, key, configurationData) }}
    {% elseif configItem.element == 'select' %}
      {{ macros.generateSelectBox(configItem.label, key, configurationData, configItem.description) }}
    {% endif %}
  </div>
{% endfor %}

<script>
  try {
    const generateUUID = () => {
      let date = new Date().getTime();

      if (window.performance && typeof window.performance.now === 'function') {
        date += performance.now();
      }

      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (char) => {
        const random = (date + Math.random() * 16) % 16 | 0;
        date = Math.floor(date / 16);
        return (char === 'x' ? random : (random & 0x3 | 0x8)).toString(16);
      });
    }

    const buttonGenerateApiToken = document.getElementById('pmf-generate-api-token');
    const inputConfigurationApiToken = document.getElementById('edit[api.apiClientToken]');

    if (buttonGenerateApiToken) {
      if (inputConfigurationApiToken.value !== '') {
        buttonGenerateApiToken.disabled = true;
      }
      buttonGenerateApiToken.addEventListener('click', (event) => {
        event.preventDefault();
        inputConfigurationApiToken.value = generateUUID();
      });
    }
  } catch (e) {
    // do nothing
  }
</script>
