<?php

declare(strict_types=1);

namespace Drupal\flowdrop_memory\Plugin\MemoryBackend;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;

/**
 * Abstract base class for FlowDrop memory backend plugins.
 *
 * Provides storage key building helpers shared by all backend implementations.
 *
 * @see \Drupal\flowdrop_memory\Plugin\MemoryBackend\MemoryBackendInterface
 */
abstract class AbstractMemoryBackend extends PluginBase implements MemoryBackendInterface, ContainerFactoryPluginInterface {

  /**
   * Builds a fully-qualified storage key from scope, scope_id, and key.
   *
   * The format is: "flowdrop_memory:{scope}:{scope_id}:{key}"
   * When scope_id is empty, "__global__" is substituted to avoid ambiguity.
   *
   * @param string $scope
   *   The memory scope.
   * @param string $scope_id
   *   The scope identifier.
   * @param string $key
   *   The memory key.
   *
   * @return string
   *   The fully-qualified storage key.
   */
  protected function buildStorageKey(string $scope, string $scope_id, string $key): string {
    return implode(':', [
      'flowdrop_memory',
      $scope,
      $scope_id !== '' ? $scope_id : '__global__',
      $key,
    ]);
  }

  /**
   * Builds the scope prefix used for listing and clearing operations.
   *
   * @param string $scope
   *   The memory scope.
   * @param string $scope_id
   *   The scope identifier.
   *
   * @return string
   *   The scope prefix string (without trailing colon).
   */
  protected function buildScopePrefix(string $scope, string $scope_id): string {
    return implode(':', [
      'flowdrop_memory',
      $scope,
      $scope_id !== '' ? $scope_id : '__global__',
    ]);
  }

}
