<?php
/**
 * File Path: includes/shortcodes/class-mbs-shortcode-registry.php
 *
 * Shortcode Registry
 *
 * Registers and manages all MBS shortcodes.
 * Updated to delegate cleanly to dedicated classes (no duplicate inline logic).
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

class MBS_Shortcode_Registry {

	public function __construct() {
		// PLG-NEW-013: This registry is instantiated DURING the 'init' action
		// (MBS_Plugin::load_services() runs at init priority 10). Deferring with
		// add_action( 'init', ... ) at the default priority 10 queues the callback
		// into the priority bucket that is currently being iterated, and PHP's
		// foreach does not see same-priority callbacks added mid-iteration -- so
		// register_shortcodes() never ran and every [mbs_*] shortcode rendered as
		// literal text in page content. Register immediately when 'init' is already
		// running or has run; otherwise hook normally.
		if ( doing_action( 'init' ) || did_action( 'init' ) ) {
			$this->register_shortcodes();
		} else {
			add_action( 'init', array( $this, 'register_shortcodes' ) );
		}
	}

	public function register_shortcodes(): void {
		add_shortcode( 'mbs_book',             array( 'MBS_Shortcode_Book', 'render' ) );
		add_shortcode( 'mbs_books',            array( 'MBS_Shortcode_Books', 'render' ) );
		add_shortcode( 'mbs_books_archive',    array( 'MBS_Shortcode_Books', 'render' ) );
		add_shortcode( 'mbs_search',           array( 'MBS_Shortcode_Search', 'render' ) );        // S13-8
		add_shortcode( 'mbs_author_directory', array( 'MBS_Shortcode_Author_Directory', 'render' ) );
		add_shortcode( 'mbs_series_directory', array( 'MBS_Shortcode_Series_Directory', 'render' ) );
		add_shortcode( 'mbs_genre_cloud',      array( 'MBS_Shortcode_Genre_Cloud', 'render' ) );   // S13-9

		// SHC-1 through SHC-5 -- single-term embeddable page shortcodes.
		add_shortcode( 'mbs_series',     array( 'MBS_Shortcode_Series',     'render' ) ); // SHC-1
		add_shortcode( 'mbs_universe',   array( 'MBS_Shortcode_Universe',   'render' ) ); // SHC-2
		add_shortcode( 'mbs_collection', array( 'MBS_Shortcode_Collection', 'render' ) ); // SHC-3
		add_shortcode( 'mbs_genre',      array( 'MBS_Shortcode_Genre',      'render' ) ); // SHC-4
		add_shortcode( 'mbs_author',     array( 'MBS_Shortcode_Author',     'render' ) ); // SHC-5
			// DISPLAY-2: Bestseller badge list -- books ranked by PA-API sales_rank. PRO.
			add_shortcode( 'mbs_bestseller', array( 'MBS_Shortcode_Bestseller', 'render' ) );

			// DISPLAY-1: Publication timeline -- vertical format + ARC date timeline. FREE.
			add_shortcode( 'mbs_timeline', array( 'MBS_Shortcode_Timeline', 'render' ) );

			// F-026-5: Reader signup form -- settings-driven, PRO only.
			add_shortcode( 'mbs_esp_signup', array( 'MBS_ESP_Widget', 'shortcode' ) );
	}
}
