<?php
/** Isolated render contracts for the narrow GenerateBlocks write surface. */

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

/** @param array<string,mixed> $block */
function wpagent_builder_block_generateblocks_text_valid( array $block ): bool {
	$name          = is_string( $block['blockName'] ?? null ) ? $block['blockName'] : '';
	$type          = WP_Block_Type_Registry::get_instance()->get_registered( $name );
	$attrs         = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
	$tag_name      = is_string( $attrs['tagName'] ?? null ) ? $attrs['tagName'] : '';
	$inner_html    = is_string( $block['innerHTML'] ?? null ) ? $block['innerHTML'] : '';
	$inner_content = $block['innerContent'] ?? null;
	$pattern       = '/\\A<' . preg_quote( $tag_name, '/' ) . ' class="gb-text">([^<>]{1,500})<\\/' . preg_quote( $tag_name, '/' ) . '>\\z/uD';
	return 'generateblocks/text' === $name
		&& $type instanceof WP_Block_Type
		&& wpagent_builder_block_render_callback_matches( $type, array( 'GenerateBlocks_Block_Text', 'render_block' ) )
		&& ! array_diff( array_keys( $attrs ), array( 'uniqueId', 'tagName' ) )
		&& is_string( $attrs['uniqueId'] ?? null ) && 1 === preg_match( '/^[A-Za-z0-9_-]{6,64}$/D', $attrs['uniqueId'] )
		&& in_array( $tag_name, wpagent_builder_block_generateblocks_text_tags(), true )
		&& isset( $block['innerBlocks'] ) && array() === $block['innerBlocks']
		&& is_array( $inner_content ) && array( $inner_html ) === $inner_content
		&& 1 === preg_match( $pattern, $inner_html, $matches )
		&& 0 === preg_match( '/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]/u', html_entity_decode( $matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) );
}

/** @param array<string,mixed> $block */
function wpagent_builder_block_generateblocks_element_valid( array $block ): bool {
	$name          = is_string( $block['blockName'] ?? null ) ? $block['blockName'] : '';
	$type          = WP_Block_Type_Registry::get_instance()->get_registered( $name );
	$attrs         = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
	$unique_id     = is_string( $attrs['uniqueId'] ?? null ) ? $attrs['uniqueId'] : '';
	$tag_name      = is_string( $attrs['tagName'] ?? null ) ? $attrs['tagName'] : '';
	$inner_html    = is_string( $block['innerHTML'] ?? null ) ? $block['innerHTML'] : '';
	$inner_content = $block['innerContent'] ?? null;
	$children      = $block['innerBlocks'] ?? null;
	$child         = is_array( $children ) && 1 === count( $children ) && is_array( $children[0] ?? null ) ? $children[0] : array();
	$child_html    = is_string( $child['innerHTML'] ?? null ) ? $child['innerHTML'] : '';
	$child_content = $child['innerContent'] ?? null;
	$pattern       = '/\\A<p>([^<>]{1,500})<\\/p>\\z/uD';
	return 'generateblocks/element' === $name
		&& $type instanceof WP_Block_Type
		&& wpagent_builder_block_render_callback_matches( $type, array( 'GenerateBlocks_Block_Element', 'render_block' ) )
		&& ! array_diff( array_keys( $attrs ), array( 'uniqueId', 'tagName' ) )
		&& 1 === preg_match( '/^[A-Za-z0-9_-]{6,64}$/D', $unique_id )
		&& in_array( $tag_name, wpagent_builder_block_generateblocks_element_tags(), true )
		&& 'core/paragraph' === ( $child['blockName'] ?? null ) && array() === ( $child['attrs'] ?? null )
		&& array() === ( $child['innerBlocks'] ?? null ) && is_array( $child_content ) && array( $child_html ) === $child_content
		&& 1 === preg_match( $pattern, $child_html, $matches )
		&& 0 === preg_match( '/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]/u', html_entity_decode( $matches[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) )
		&& '<' . $tag_name . ' class="gb-element gb-element-' . $unique_id . '"></' . $tag_name . '>' === $inner_html
		&& is_array( $inner_content ) && array( '<' . $tag_name . ' class="gb-element gb-element-' . $unique_id . '">', null, '</' . $tag_name . '>' ) === $inner_content;
}

/** @param array<string,mixed> $block */
function wpagent_builder_block_generateblocks_query_context_valid( array $block ): bool {
	$attrs = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
	$query = $attrs['query'] ?? null;
	if ( array_diff( array_keys( $attrs ), array( 'uniqueId', 'tagName', 'queryType', 'paginationType', 'query', 'inheritQuery' ) )
		|| ! is_string( $attrs['uniqueId'] ?? null ) || ! preg_match( '/^[A-Za-z0-9_-]{6,64}$/D', $attrs['uniqueId'] )
		|| ! in_array( $attrs['tagName'] ?? null, wpagent_builder_block_generateblocks_query_tags(), true ) || 'WP_Query' !== ( $attrs['queryType'] ?? null )
		|| ! in_array( $attrs['paginationType'] ?? null, array( 'standard', 'instant' ), true ) || false !== ( $attrs['inheritQuery'] ?? null )
		|| ! is_array( $query ) || wp_is_numeric_array( $query ) || array_diff( array_keys( $query ), array( 'post_type', 'post_status', 'posts_per_page' ) )
		|| 'post' !== ( $query['post_type'] ?? null ) || 'publish' !== ( $query['post_status'] ?? null )
		|| ! is_int( $query['posts_per_page'] ?? null ) || $query['posts_per_page'] < 1 || $query['posts_per_page'] > 100 ) {
		return false;
	}
	return true;
}

/** @param array<string,mixed> $block */
function wpagent_builder_block_generateblocks_page_numbers_valid( array $block ): bool {
	$name          = is_string( $block['blockName'] ?? null ) ? $block['blockName'] : '';
	$type          = WP_Block_Type_Registry::get_instance()->get_registered( $name );
	$attrs         = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
	$tag_name      = is_string( $attrs['tagName'] ?? null ) ? $attrs['tagName'] : '';
	$inner_html    = is_string( $block['innerHTML'] ?? null ) ? $block['innerHTML'] : '';
	$inner_content = $block['innerContent'] ?? null;
	return 'generateblocks/query-page-numbers' === $name
		&& $type instanceof WP_Block_Type
		&& wpagent_builder_block_render_callback_matches( $type, array( 'GenerateBlocks_Block_Query_Page_Numbers', 'render_block' ) )
		&& ! array_diff( array_keys( $attrs ), array( 'uniqueId', 'tagName', 'midSize' ) )
		&& is_string( $attrs['uniqueId'] ?? null ) && 1 === preg_match( '/^[A-Za-z0-9_-]{6,64}$/D', $attrs['uniqueId'] )
		&& in_array( $tag_name, wpagent_builder_block_generateblocks_page_number_tags(), true )
		&& is_int( $attrs['midSize'] ?? null ) && $attrs['midSize'] >= 0 && $attrs['midSize'] <= 10
		&& isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) && array() === $block['innerBlocks']
		&& '<' . $tag_name . ' class="gb-query-page-numbers"></' . $tag_name . '>' === $inner_html
		&& is_array( $inner_content ) && 1 === count( $inner_content ) && $inner_html === $inner_content[0];
}

/** @param array<string,mixed> $block */
function wpagent_builder_block_generateblocks_query_branch_valid( array $block ): bool {
	$name          = is_string( $block['blockName'] ?? null ) ? $block['blockName'] : '';
	$type          = WP_Block_Type_Registry::get_instance()->get_registered( $name );
	$attrs         = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
	$tag_name      = is_string( $attrs['tagName'] ?? null ) ? $attrs['tagName'] : '';
	$children      = $block['innerBlocks'] ?? null;
	$inner_html    = is_string( $block['innerHTML'] ?? null ) ? $block['innerHTML'] : '';
	$inner_content = $block['innerContent'] ?? null;
	return 'generateblocks/query' === $name
		&& $type instanceof WP_Block_Type
		&& wpagent_builder_block_render_callback_matches( $type, array( 'GenerateBlocks_Block_Query', 'render_block' ) )
		&& wpagent_builder_block_generateblocks_query_context_valid( $block )
		&& is_array( $children ) && 1 === count( $children ) && is_array( $children[0] ?? null )
		&& wpagent_builder_block_generateblocks_page_numbers_valid( $children[0] )
		&& in_array( $tag_name, wpagent_builder_block_generateblocks_query_tags(), true )
		&& '<' . $tag_name . ' class="gb-query"></' . $tag_name . '>' === $inner_html
		&& is_array( $inner_content ) && 3 === count( $inner_content )
		&& '<' . $tag_name . ' class="gb-query">' === ( $inner_content[0] ?? null )
		&& null === $inner_content[1]
		&& '</' . $tag_name . '>' === ( $inner_content[2] ?? null )
		&& $inner_html === $inner_content[0] . $inner_content[2];
}
