<?php
/**
 * Lightweight card variant map (not a Layout Engine).
 *
 * @package Kutby
 * @since   2.9.0
 */

declare(strict_types=1);

namespace Kutby\Cards\Services;

use Kutby\Cards\Contracts\CardVariantInterface;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class CardRegistry
 */
final class CardRegistry {

	/**
	 * @var array<string, class-string<CardVariantInterface>>
	 */
	private array $variants = array();

	/**
	 * @var self|null
	 */
	private static ?self $instance = null;

	/**
	 * @return self
	 */
	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * @param string                             $id    Id.
	 * @param class-string<CardVariantInterface> $class Class.
	 * @return void
	 */
	public function add( string $id, string $class ): void {
		$this->variants[ sanitize_key( $id ) ] = $class;
	}

	/**
	 * @param string $id Id.
	 * @return bool
	 */
	public function has( string $id ): bool {
		return isset( $this->variants[ sanitize_key( $id ) ] );
	}

	/**
	 * @param string $id Id.
	 * @return class-string<CardVariantInterface>|null
	 */
	public function get( string $id ): ?string {
		return $this->variants[ sanitize_key( $id ) ] ?? null;
	}

	/**
	 * @return array<string, class-string<CardVariantInterface>>
	 */
	public function all(): array {
		return $this->variants;
	}
}
