<?php
/**
 * Block Presets - Unified preset system for Brandy blocks and patterns
 *
 * Two data providers feed one modal UI:
 * 1. Registry: presets registered via brandy_register_block_preset()
 * 2. Pattern: auto-detected from metadata.patternName + categories
 *
 * @package Brandy\BlockSettings\BlockPresets
 */

namespace Brandy\BlockSettings\BlockPresets;

use Brandy\Traits\SingletonTrait;

class Caller {
	use SingletonTrait;

	/**
	 * Registry of block presets keyed by block name.
	 * Structure: [ 'block/name' => [ preset1, preset2, ... ] ]
	 *
	 * @var array
	 */
	private static $presets = array();

	/**
	 * Register a preset for a block type.
	 *
	 * @param string $block_name Block name (e.g., 'brandy/carousel', 'core/group').
	 * @param array  $preset     Preset data with keys: name, title, content, description, viewportWidth.
	 */
	public static function register_preset( $block_name, $preset ) {
		if ( empty( $preset['name'] ) || empty( $preset['content'] ) ) {
			return;
		}

		$preset = wp_parse_args(
			$preset,
			array(
				'title'         => $preset['name'],
				'description'   => '',
				'viewportWidth' => 1200,
			)
		);

		self::$presets[ $block_name ][] = $preset;
	}

	/**
	 * Get all registered presets.
	 *
	 * @return array
	 */
	public static function get_presets() {
		return self::$presets;
	}

	protected function __construct() {
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_settings_scripts' ) );
	}

	public function enqueue_settings_scripts() {
		wp_enqueue_script(
			'brandy/block-presets',
			BRANDY_TEMPLATE_URL . '/inc/BlockSettings/BlockPresets/script.js',
			array(
				'wp-edit-post',
				'wp-blocks',
				'wp-element',
				'wp-components',
				'wp-block-editor',
				'wp-compose',
				'wp-hooks',
				'wp-i18n',
				'wp-data',
			),
			BRANDY_SCRIPT_VERSION,
			true
		);

		// Localize registry presets to JS
		wp_localize_script( 'brandy/block-presets', 'brandyBlockPresets', self::$presets );
	}
}

Caller::get_instance();

// Load global registration function
require_once __DIR__ . '/functions.php';

// Load demo presets (remove after testing)
require_once __DIR__ . '/demo-presets.php';
