<?php
/**
 * Register block scripts and styles.
 *
 * @package {{phpClassName}}
 */

namespace {{phpClassName}};

/**
 * Register blocks.
 *
 * @since {{version}}
 */
class RegisterBlocks {

	/**
	 * Register class with appropriate WordPress hooks
	 */
	public static function register() {
		$instance = new self();
		add_action( 'init', array( $instance, 'register_blocks' ) );
	}

	/**
	 * Registers scripts and styles so they can be enqueued through Gutenberg. IMPORTANT - Registers scripts and styles for server rendered ( dynamic ) blocks as well.
	 *
	 * @return void
	 */
	public function register_blocks() {

		if ( ! function_exists( 'register_block_type' ) ) {
			// Gutenberg is not active.
			return;
		}

		// Register block specific frontend & backend styles.
		wp_register_style(
			{{slugConstant}}_PLUGIN_SLUG . '-block',
			{{slugConstant}}_PLUGIN_URL . 'build/style-index.css',
			array(),
			{{slugConstant}}_PLUGIN_VERSION
		);

		// Register editor-only block styles.
		wp_register_style(
			{{slugConstant}}_PLUGIN_SLUG . '-block-editor',
			{{slugConstant}}_PLUGIN_URL . 'build/index.css',
			array( 'wp-edit-blocks' ),
			{{slugConstant}}_PLUGIN_VERSION
		);
		{{#registerFrontend}}

		// Register frontend scripts.
		// Dynamically load dependencies using frontend.asset.php generated by @wordpress/dependency-extraction-webpack-plugin.
		$frontend_script_asset_path = {{slugConstant}}_PLUGIN_PATH . '/build/frontend.asset.php';
		if ( ! file_exists( $frontend_script_asset_path ) ) {
			throw new \Error(
				'You need to run `npm start` or `npm run build` for the "test" blocks first.'
			);
		}

		$frontend_script_asset = require $frontend_script_asset_path;

		wp_register_script(
			{{slugConstant}}_PLUGIN_SLUG . '-frontend',
			{{slugConstant}}_PLUGIN_URL . 'build/frontend.js',
			$frontend_script_asset['dependencies'],
			$frontend_script_asset['version'],
			true
		);
		{{/registerFrontend}}

		// Register editor-only block scripts.
		// Dynamically load dependencies using index.build.asset.php generated by @wordpress/dependency-extraction-webpack-plugin.
		$script_asset_path = {{slugConstant}}_PLUGIN_PATH . 'build/index.asset.php';
		if ( ! file_exists( $script_asset_path ) ) {
			throw new \Error(
				'You need to run `npm start` or `npm run build` for the "{{slug}}" blocks first.'
			);
		}

		$script_asset = require $script_asset_path;

		wp_register_script(
			{{slugConstant}}_PLUGIN_SLUG . '-block-editor',
			{{slugConstant}}_PLUGIN_URL . 'build/index.js',
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		$block_directories = scandir( {{slugConstant}}_PLUGIN_PATH . 'src/blocks/' );
		// Using scandir picks up . & .. in linux environments, here we're cleaning the array of $block_directories.
		$block_directories = array_diff( $block_directories, array( '..', '.', '.DS_Store' ) );

		// Automatically register blocks.
		// Do not register dynamic block's here, this is done directly in dynamic block .php file.
		foreach ( $block_directories as $block ) {
			{{#load_dynamic_blocks}}
			$dynamic_block = false;

			foreach ( glob( {{slugConstant}}_PLUGIN_PATH . "src/blocks/$block/*.php" ) as $block_file ) {
				$dynamic_block = true;
			};

			if ( $dynamic_block ) {
				continue;
			}
			
			{{/load_dynamic_blocks}}
			// Register blocks.
			// For reference: https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/.
			register_block_type(
				{{slugConstant}}_PLUGIN_SLUG . "/$block",
				array(
					'style'         => {{slugConstant}}_PLUGIN_SLUG . '-block',
					'editor_style'  => {{slugConstant}}_PLUGIN_SLUG . '-block-editor',
					{{#registerFrontend}}
					'script'        => {{slugConstant}}_PLUGIN_SLUG . '-frontend',
					{{/registerFrontend}}
					'editor_script' => {{slugConstant}}_PLUGIN_SLUG . '-block-editor',
				)
			);
		};
	}
}
