<?php
/**
 * Block Gutenberg — dynamic render giống shortcode.
 *
 * @package DatvetaucaotocBooking
 */

declare(strict_types=1);

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Datvetau_Booking_Block {

	public static function register(): void {
		add_action( 'init', array( self::class, 'register_block' ) );
	}

	public static function register_block(): void {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		wp_register_script(
			'datvetau-booking-block-editor',
			DATVETAU_BOOKING_URL . 'assets/js/block-editor.js',
			array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n' ),
			DATVETAU_BOOKING_VERSION,
			true
		);

		register_block_type(
			'datvetau/booking-wizard',
			array(
				'api_version'     => 3,
				'title'           => __( 'Đặt vé tàu', 'datvetaucaotoc-booking' ),
				'description'     => __( 'Wizard đặt vé 4 bước (Datvetaucaotoc).', 'datvetaucaotoc-booking' ),
				'category'        => 'widgets',
				'icon'            => 'tickets-alt',
				'editor_script'   => 'datvetau-booking-block-editor',
				'attributes'      => array(
					'route' => array(
						'type'    => 'string',
						'default' => '',
					),
				),
				'render_callback' => array( self::class, 'render' ),
				'supports'        => array(
					'html' => false,
				),
			)
		);
	}

	/**
	 * @param array<string,mixed> $attributes
	 */
	public static function render( array $attributes ): string {
		$route = isset( $attributes['route'] ) ? (string) $attributes['route'] : '';
		return Datvetau_Booking_Shortcode::render( array( 'route' => $route ) );
	}
}
