<?php

declare(strict_types=1);

namespace Drupal\flowdrop_stategraph\Plugin\FlowDropNodeProcessor;

use Drupal\flowdrop_stategraph\DTO\GraphState;

/**
 * Standard implementation of {@see StateAwareProcessorInterface}.
 *
 * Stores the injected graph state and exposes it to the processor. The
 * state is NULL when the node runs outside a StateGraph orchestration
 * (or setGraphState() was never called), so processors must handle the
 * stateless case — typically by falling back to defaults.
 *
 * @api
 */
trait StateAwareProcessorTrait {

  /**
   * The current graph state, injected by the StateGraph orchestrator.
   *
   * @var \Drupal\flowdrop_stategraph\DTO\GraphState|null
   */
  protected ?GraphState $graphState = NULL;

  /**
   * {@inheritdoc}
   */
  public function setGraphState(GraphState $state): void {
    $this->graphState = $state;
  }

  /**
   * Gets the current graph state.
   *
   * @return \Drupal\flowdrop_stategraph\DTO\GraphState|null
   *   The graph state, or NULL when running outside a StateGraph
   *   orchestration.
   */
  protected function getGraphState(): ?GraphState {
    return $this->graphState;
  }

}
