<?php

declare(strict_types=1);

namespace Drupal\flowdrop_node_processor\Plugin\FlowDropNodeProcessor;

use Drupal\flowdrop\Attribute\FlowDropNodeProcessor;
use Drupal\flowdrop\DTO\ParameterBagInterface;
use Drupal\flowdrop\DTO\ValidationResult;
use Drupal\flowdrop\Plugin\FlowDropNodeProcessor\AbstractFlowDropNodeProcessor;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Applies a common text utility to a string input.
 *
 * Supports capitalize, uppercase, lowercase, trim and strip_tags. The
 * operation can be set as configuration on the node or piped in via a port,
 * which makes the node useful both as a fixed transform and as a step whose
 * behaviour depends on upstream nodes.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
#[FlowDropNodeProcessor(
  id: "text_processor",
  label: new \Drupal\Core\StringTranslation\TranslatableMarkup("Text Processor"),
  description: "Apply a common text utility (capitalize, uppercase, lowercase, trim, strip_tags) to a string",
  version: "1.0.0"
)]
class TextProcessor extends AbstractFlowDropNodeProcessor {

  private const OPERATIONS = [
    'capitalize',
    'uppercase',
    'lowercase',
    'trim',
    'strip_tags',
  ];

  private const DEFAULT_OPERATION = 'capitalize';

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ): static {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function validateParams(array $params): ValidationResult {
    return ValidationResult::success();
  }

  /**
   * {@inheritdoc}
   */
  public function process(ParameterBagInterface $params): array {
    $text = $params->getString('text');
    $operation = $params->getString('operation', self::DEFAULT_OPERATION);

    if (!in_array($operation, self::OPERATIONS, TRUE)) {
      $operation = self::DEFAULT_OPERATION;
    }

    $result = match ($operation) {
      'capitalize' => mb_convert_case($text, MB_CASE_TITLE, 'UTF-8'),
      'uppercase' => mb_strtoupper($text, 'UTF-8'),
      'lowercase' => mb_strtolower($text, 'UTF-8'),
      'trim' => trim($text),
      'strip_tags' => strip_tags($text),
    };

    return [
      'text' => $result,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getParameterSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'text' => [
          'type' => 'string',
          'title' => 'Text',
          'description' => 'The string to transform',
          'default' => '',
        ],
        'operation' => [
          'type' => 'string',
          'title' => 'Operation',
          'description' => 'Which transformation to apply',
          'default' => self::DEFAULT_OPERATION,
          'enum' => self::OPERATIONS,
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputSchema(): array {
    return [
      'type' => 'object',
      'properties' => [
        'text' => [
          'type' => 'string',
          'description' => 'The transformed text',
        ],
      ],
    ];
  }

}
