<?php
/** Exact provider contract for the bounded Kadence Spacer Divider writer. */

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

/** @param array<string,mixed> $block */
function wpagent_builder_block_kadence_spacer_valid( array $block ): bool {
	$name          = is_string( $block['blockName'] ?? null ) ? $block['blockName'] : '';
	$type          = WP_Block_Type_Registry::get_instance()->get_registered( $name );
	$callback      = $type instanceof WP_Block_Type ? $type->render_callback : null;
	$attrs         = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();
	$allowed_attrs = array(
		'uniqueID', 'spacerHeight', 'spacerHeightUnits', 'tabletSpacerHeight', 'mobileSpacerHeight',
		'dividerEnable', 'dividerStyle', 'dividerOpacity', 'dividerColor', 'dividerWidth',
		'dividerWidthUnits', 'tabletDividerWidth', 'mobileDividerWidth', 'dividerHeight',
		'tabletDividerHeight', 'mobileDividerHeight',
	);
	$unique_id       = is_string( $attrs['uniqueID'] ?? null ) ? $attrs['uniqueID'] : '';
	$inner_html      = is_string( $block['innerHTML'] ?? null ) ? $block['innerHTML'] : '';
	$inner_content   = $block['innerContent'] ?? null;
	$divider_enabled = $attrs['dividerEnable'] ?? null;
	$height_keys     = array( 'spacerHeight', 'tabletSpacerHeight', 'mobileSpacerHeight' );
	foreach ( $height_keys as $height_key ) {
		if ( array_key_exists( $height_key, $attrs ) && ( ! is_int( $attrs[ $height_key ] ) || $attrs[ $height_key ] < 1 || $attrs[ $height_key ] > 2000 ) ) {
			return false;
		}
	}
	$divider_height_keys = array( 'dividerHeight', 'tabletDividerHeight', 'mobileDividerHeight' );
	foreach ( $divider_height_keys as $height_key ) {
		if ( array_key_exists( $height_key, $attrs ) && ( ! is_int( $attrs[ $height_key ] ) || $attrs[ $height_key ] < 1 || $attrs[ $height_key ] > 400 ) ) {
			return false;
		}
	}
	if ( ! is_bool( $divider_enabled ) ) {
		return false;
	}
	$divider_fields = array(
		'dividerStyle', 'dividerOpacity', 'dividerColor', 'dividerWidth', 'dividerWidthUnits',
		'tabletDividerWidth', 'mobileDividerWidth', 'dividerHeight', 'tabletDividerHeight', 'mobileDividerHeight',
	);
	if ( ! $divider_enabled && array_intersect( array_keys( $attrs ), $divider_fields ) ) {
		return false;
	}
	if ( $divider_enabled ) {
		if ( array_key_exists( 'dividerStyle', $attrs ) && ! in_array( $attrs['dividerStyle'], array( 'solid', 'dashed', 'dotted' ), true ) ) {
			return false;
		}
		if ( array_key_exists( 'dividerOpacity', $attrs ) && ( ! is_int( $attrs['dividerOpacity'] ) || $attrs['dividerOpacity'] < 0 || $attrs['dividerOpacity'] > 100 ) ) {
			return false;
		}
		if ( array_key_exists( 'dividerColor', $attrs ) && ( ! is_string( $attrs['dividerColor'] ) || 1 !== preg_match( '/^#[a-f0-9]{6}$/Di', $attrs['dividerColor'] ) ) ) {
			return false;
		}
		if ( array_key_exists( 'dividerWidthUnits', $attrs ) && ! in_array( $attrs['dividerWidthUnits'], array( 'px', '%' ), true ) ) {
			return false;
		}
		$divider_units = $attrs['dividerWidthUnits'] ?? '%';
		foreach ( array( 'dividerWidth', 'tabletDividerWidth', 'mobileDividerWidth' ) as $width_key ) {
			if ( array_key_exists( $width_key, $attrs ) && ( ! is_int( $attrs[ $width_key ] ) || $attrs[ $width_key ] < 1 || $attrs[ $width_key ] > ( '%' === $divider_units ? 100 : 3000 ) ) ) {
				return false;
			}
		}
	}
	$expected_html = '<div class="wp-block-kadence-spacer aligncenter kt-block-spacer-' . $unique_id . '"><div class="kt-block-spacer kt-block-spacer-halign-center">' . ( $divider_enabled ? '<hr class="kt-divider" />' : '' ) . '</div></div>';
	return 'kadence/spacer' === $name
		&& $type instanceof WP_Block_Type
		&& is_array( $callback ) && 2 === count( $callback )
		&& isset( $callback[0] ) && $callback[0] instanceof Kadence_Blocks_Spacer_Block
		&& 'render_css' === ( $callback[1] ?? null ) && is_callable( $callback )
		&& ! array_diff( array_keys( $attrs ), $allowed_attrs )
		&& 1 === preg_match( '/^[A-Za-z0-9_-]{6,64}$/D', $unique_id )
		&& is_int( $attrs['spacerHeight'] ?? null )
		&& in_array( $attrs['spacerHeightUnits'] ?? 'px', array( 'px', 'rem', 'vh' ), true )
		&& isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) && array() === $block['innerBlocks']
		&& $expected_html === $inner_html
		&& is_array( $inner_content ) && array( $expected_html ) === $inner_content;
}
