<?php
/**
 * Block editor init
 *
 * @author     AnimaGate, Inc.
 * @link       https://support.animagate.com/product/wp-nishiki-pro/
 */

/**
 * Assets
 *
 * @return void
 */
function nishiki_blocks_pro_v1_init() {
	global $pagenow;

	// Scripts.
	$block_dependencies = array(
		'wp-blocks',
		'wp-i18n',
		'wp-element',
		'wp-components',
		'wp-compose',
		'wp-rich-text',
		'wp-data',
		'wp-plugins',
		'wp-hooks',
		'lodash',
	);

	wp_register_script(
		'nishiki-blocks-pro-v1',
		get_template_directory_uri() . '/extension/block/build/index.js',
		$block_dependencies,
		true, // Enqueue the script in the footer.
		'1.0.0'
	);

	if ( function_exists( 'register_block_type' ) ) {
		register_block_type(
			'nishiki-blocks-pro/blocks',
			array(
				'editor_script' => 'nishiki-blocks-pro-v1',
				'editor_style'  => 'nishiki-blocks-pro-v1',
			)
		);
	}

}

// Hook: Frontend and Editor assets.
add_action( 'init', 'nishiki_blocks_pro_v1_init' );

// Add Block Category.
if ( ! function_exists( 'nishiki_blocks_pro_categories' ) ) {
	/**
	 * ブロックカテゴリー追加
	 *
	 * @param array $categories Array of categories for block types.
	 */
	function nishiki_blocks_pro_categories( $categories ) {
		array_unshift( $categories, array(
			'slug'  => 'nishiki-blocks-pro-deplecated',
			'title' => __( '非推奨', 'nishiki-pro' )
		) );

		array_unshift( $categories, array(
			'slug'  => 'nishiki-blocks-pro',
			'title' => __( 'Nishiki Blocks Pro', 'nishiki-pro' )
		) );

		return $categories;
	}
	if ( function_exists( 'get_default_block_categories' ) && function_exists( 'get_block_editor_settings' ) ) {
		add_filter( 'block_categories_all', 'nishiki_blocks_pro_categories', 10, 1 );
	} else {
		add_filter( 'block_categories', 'nishiki_blocks_pro_categories', 10, 1 );
	}
}
