<?php
/**
 * FoodOrd Table Reservations block.
 *
 * @package FoodOrd
 */

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

/**
 * Class Foodord_Table_Reservations_Block
 */
class Foodord_Table_Reservations_Block {
	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'register_block' ) );
	}

	/**
	 * Register dynamic block.
	 */
	public function register_block(): void {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		wp_register_script(
			'foodord-table-reservations-block-editor',
			FOODORD_URL . 'admin/js/blocks/foodord-table-reservations-block.js',
			array( 'wp-blocks', 'wp-element', 'wp-i18n' ),
			FOODORD_VERSION,
			true
		);

		wp_register_style(
			'foodord-table-reservations-block-editor',
			FOODORD_URL . 'admin/css/blocks/foodord-products-block-editor.css',
			array( 'wp-edit-blocks' ),
			FOODORD_VERSION
		);

		register_block_type(
			'foodord/reservations',
			array(
				'render_callback' => array( $this, 'render' ),
				'editor_script'   => 'foodord-table-reservations-block-editor',
				'editor_style'    => 'foodord-table-reservations-block-editor',
			)
		);
	}

	/**
	 * Render block.
	 *
	 * @return string
	 */
	public function render(): string {
		if ( ! class_exists( 'Foodord_Table_Reservations_Frontend' ) ) {
			return '';
		}

		$GLOBALS['foodord_has_table_reservations_block'] = true;

		$frontend = new Foodord_Table_Reservations_Frontend();

		return $frontend->render_shortcode();
	}
}
