<?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;
		}

		// Shortcuts for variables.
		$plugin_instance = Plugin::get_instance();
		$slug            = $plugin_instance->slug;
		$plugin_dir_path = $plugin_instance->plugin_dir_path;
		$plugin_dir_url  = $plugin_instance->plugin_dir_url;
		$version         = $plugin_instance->version;

		// Register block specific frontend & backend styles.
		wp_register_style(
			$slug . '-block',
			$plugin_dir_url . 'build/style-index.css',
			array(),
			$version
		);

		// Register editor-only block styles.
		wp_register_style(
			$slug . '-block-editor',
			$plugin_dir_url . 'build/index.css',
			array( 'wp-edit-blocks' ),
			$version
		);
		{{#registerFrontend}}

		// Register frontend scripts.
		// Dynamically load dependencies using frontend.asset.php generated by @wordpress/dependency-extraction-webpack-plugin.
		$frontend_script_asset_path = $plugin_dir_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(
			$slug . '-frontend',
			$plugin_dir_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 = $plugin_dir_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(
			$slug . '-block-editor',
			$plugin_dir_url . 'build/index.js',
			$script_asset['dependencies'],
			$script_asset['version'],
			true
		);

		$blocks            = array();
		$block_directories = scandir( $plugin_dir_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 ) {
			$dynamic_block = false;

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

			if ( $dynamic_block ) {
				continue;
			}
			$blocks[] = "$slug/$block";
		};

		// Loop through $blocks array and register blocks.
		// For reference: https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/.
		foreach ( $blocks as $block ) {
			register_block_type(
				$block,
				array(
					'style'         => $slug . '-block',
					'editor_style'  => $slug . '-block-editor',
					{{#registerFrontend}}
					'script'        => $slug . '-frontend',
					{{/registerFrontend}}
					'editor_script' => $slug . '-block-editor',
				)
			);
		}
	}
}
