<?php

/**
 * @file
 * Provides development tools Webform module.
 */

use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Utility\WebformYaml;

/**
 * Implements hook_webform_help_info().
 */
function webform_devel_webform_help_info() {
  $help = [];
  $help['webform_devel_schema'] = [
    'group' => 'development',
    'title' => t('Devel: Webform Schema'),
    'content' => t("The <strong>Schema</strong> page displays an overview of a webform's elements and specified data types, which can be used to map webform submissions to an external API."),
    'video_id' => 'development',
    'routes' => [
      // @see /admin/structure/webform/manage/{webform}/schema
      'entity.webform.schema_form',
    ],
  ];
  $help['webform_devel_form_api_export'] = [
    'group' => 'forms',
    'title' => t('Form API Export'),
    'content' => t("The <strong>Form API export</strong> page demonstrates how a webform's elements may be used to create custom configuration forms."),
    'routes' => [
      // @see /admin/structure/webform/manage/{webform}/fapi
      'entity.webform.fapi_export_form',
    ],
  ];
  return $help;
}

/**
 * Implements hook_entity_type_alter().
 */
function webform_devel_entity_type_alter(array &$entity_types) {
  if (isset($entity_types['webform'])) {
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
    $entity_type = $entity_types['webform'];
    $handlers = $entity_type->getHandlerClasses();
    $handlers['form']['schema'] = 'Drupal\webform_devel\Form\WebformDevelEntitySchemaForm';
    $handlers['form']['fapi_export'] = 'Drupal\webform_devel\Form\WebformDevelEntityFormApiExportForm';
    $handlers['form']['fapi_test'] = 'Drupal\webform_devel\Form\WebformDevelEntityFormApiTestForm';
    $entity_type->setHandlerClass('form', $handlers['form']);
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for config single export form.
 */
function webform_devel_form_config_single_export_form_alter(&$form, FormStateInterface $form_state) {
  $form['export']['#type'] = 'webform_codemirror';
  $form['export']['#mode'] = 'yaml';

  $form['config_name']['#ajax']['callback'] = '_webform_form_config_single_export_form_update_export';
}

/**
 * Handles switching the export textarea and tidies exported MSK configuration.
 *
 * Copied from: \Drupal\config\Form\ConfigSingleExportForm::updateExport.
 */
function _webform_form_config_single_export_form_update_export($form, FormStateInterface $form_state) {
  // Determine the full config name for the selected config entity.
  if ($form_state->getValue('config_type') !== 'system.simple') {
    $definition = \Drupal::entityTypeManager()->getDefinition($form_state->getValue('config_type'));
    $name = $definition->getConfigPrefix() . '.' . $form_state->getValue('config_name');
  }
  // The config name is used directly for simple configuration.
  else {
    $name = $form_state->getValue('config_name');
  }

  // Read the raw data for this config name, encode it, and display it.
  $value = Yaml::encode(\Drupal::service('config.storage')->read($name));

  // Tidy all only Webform exported configuration…for now.
  if (strpos($name, 'webform') === 0) {
    $value = WebformYaml::tidy($value);
  }

  $form['export']['#type'] = 'webform_codemirror';
  $form['export']['#mode'] = 'yaml';
  $form['export']['#description'] = t('Filename: %name', ['%name' => $name . '.yml']);
  $form['export']['#value'] = $value;

  return $form['export'];
}
