<?php

declare(strict_types=1);

namespace Drupal\ai\Plugin;

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Plugin\PluginBase;

/**
 * Base class for configurable plugins.
 *
 * This class serves as a compatibility shim for Drupal 11.2. It is a copy of
 * \Drupal\Core\Plugin\ConfigurablePluginBase with
 * \Drupal\Core\Plugin\ConfigurableTrait inlined, as the core class is not yet
 * available in Drupal 11.2.
 *
 * @internal
 *
 * @todo Remove this class and switch to \Drupal\Core\Plugin\ConfigurablePluginBase
 *   when the module's minimum core version is raised to 11.3.
 */
abstract class ConfigurablePluginBase extends PluginBase implements ConfigurableInterface {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeepArray([$this->defaultConfiguration(), $configuration], TRUE);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

}
