<?php
/**
 * Gutenberg block: a server-rendered wrapper around the renderer.
 *
 * @package DataStream
 */

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

/**
 * Class DataStream_Block
 */
class DataStream_Block {

	/**
	 * Hook block registration.
	 */
	public static function register() {
		add_action( 'init', array( __CLASS__, 'register_block' ) );
	}

	/**
	 * Register the editor script and the block type.
	 */
	public static function register_block() {
		if ( ! function_exists( 'register_block_type' ) ) {
			return;
		}

		wp_register_script(
			'datastream-block',
			plugins_url( 'assets/js/datastream-block.js', DATASTREAM_PLUGIN_FILE ),
			array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render', 'wp-i18n' ),
			DATASTREAM_VERSION,
			true
		);

		register_block_type(
			'datastream/list',
			array(
				'api_version'     => 2,
				'editor_script'   => 'datastream-block',
				'render_callback' => array( __CLASS__, 'render' ),
				'attributes'      => array(
					'stream'     => array(
						'type'    => 'string',
						'default' => '',
					),
					'limit'      => array(
						'type'    => 'number',
						'default' => 10,
					),
					'template'   => array(
						'type'    => 'string',
						'default' => 'list',
					),
					'fields'     => array(
						'type'    => 'string',
						'default' => '',
					),
					'pageSize'   => array(
						'type'    => 'number',
						'default' => 0,
					),
					'orderby'    => array(
						'type'    => 'string',
						'default' => '',
					),
					'order'      => array(
						'type'    => 'string',
						'default' => 'desc',
					),
					'searchable' => array(
						'type'    => 'boolean',
						'default' => false,
					),
					'style'      => array(
						'type'    => 'string',
						'default' => '',
					),
				),
			)
		);
	}

	/**
	 * Server-side render callback.
	 *
	 * @param array<string, mixed> $attributes Block attributes.
	 * @return string
	 */
	public static function render( $attributes ) {
		$html = DataStream_Renderer::render_list(
			array(
				'stream'     => (string) ( $attributes['stream'] ?? '' ),
				'limit'      => absint( $attributes['limit'] ?? 10 ),
				'template'   => (string) ( $attributes['template'] ?? 'list' ),
				'fields'     => (string) ( $attributes['fields'] ?? '' ),
				'page_size'  => absint( $attributes['pageSize'] ?? 0 ),
				'orderby'    => (string) ( $attributes['orderby'] ?? '' ),
				'order'      => (string) ( $attributes['order'] ?? 'desc' ),
				'searchable' => ! empty( $attributes['searchable'] ),
				'style'      => (string) ( $attributes['style'] ?? '' ),
			)
		);

		// In the editor preview, a blank list is confusing: explain it.
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST
			&& false !== strpos( $html, 'datastream-empty-state' )
			&& current_user_can( 'edit_posts' ) ) {
			$html .= '<p class="datastream-editor-hint">'
				. esc_html__( 'Editor note: synced records appear here. If this stays empty, run a sync on the stream or check its name.', 'datastream-api-sync' )
				. '</p>';
		}

		return $html;
	}
}
