<?php

declare(strict_types=1);

namespace Drupal\flowdrop\Plugin\FlowDropNodeProcessor;

use Drupal\flowdrop\DTO\Tool\ToolCollection;
use Drupal\flowdrop\DTO\Tool\ToolCollectionInterface;

/**
 * Standard implementation of {@see ToolsAwareInterface}.
 *
 * Stores the injected tools and exposes them to the processor. Defaults to an
 * empty collection so process()/resume() can call getConnectedTools() safely
 * even when no tools are wired (or setTools() was never called).
 *
 * @api
 */
trait ToolsAwareTrait {

  /**
   * The tools wired to this node.
   *
   * @var \Drupal\flowdrop\DTO\Tool\ToolCollectionInterface
   */
  protected ToolCollectionInterface $tools;

  /**
   * {@inheritdoc}
   */
  public function setTools(ToolCollectionInterface $tools): void {
    $this->tools = $tools;
  }

  /**
   * Gets the tool collection.
   *
   * @return \Drupal\flowdrop\DTO\Tool\ToolCollectionInterface
   *   The tools wired to this node (empty if none).
   */
  protected function getTools(): ToolCollectionInterface {
    return $this->tools ??= new ToolCollection();
  }

  /**
   * Gets the connected tool bindings.
   *
   * @return array<int, \Drupal\flowdrop\DTO\Tool\ToolBindingInterface>
   *   The tool bindings wired to this node (empty if none).
   */
  protected function getConnectedTools(): array {
    return $this->getTools()->all();
  }

}
