<?php
/** Exact provider contract for the bounded Kadence Progress Bar writer. */

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

/** @param array<string,mixed> $block */
function wpagent_builder_block_kadence_progress_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', 'barType', 'hAlign', 'thAlign', 'mhAlign', 'progressAmount', 'progressMax', 'progressWidth',
		'progressWidthTablet', 'progressWidthMobile', 'progressColor', 'barBackground',
		'displayLabel', 'label', 'ariaLabel', 'numberPrefix', 'numberSuffix', 'labelPosition',
		'decimal', 'displayPercent', 'delayUntilInView',
	);
	$unique_id     = is_string( $attrs['uniqueID'] ?? null ) ? $attrs['uniqueID'] : '';
	$amount        = $attrs['progressAmount'] ?? null;
	$maximum       = $attrs['progressMax'] ?? null;
	$widths        = array( 'progressWidth', 'progressWidthTablet', 'progressWidthMobile' );
	if ( ! is_int( $amount ) || ! is_int( $maximum ) || $amount < 0 || $amount > 100 || $maximum < 1 || $maximum > 100 || $amount > $maximum ) {
		return false;
	}
	foreach ( $widths as $width ) {
		if ( array_key_exists( $width, $attrs ) && ( ! is_int( $attrs[ $width ] ) || $attrs[ $width ] < 1 || $attrs[ $width ] > 20 ) ) {
			return false;
		}
	}
	foreach ( array( 'hAlign', 'thAlign', 'mhAlign' ) as $alignment ) {
		$allowed = 'hAlign' === $alignment ? array( 'space-between', 'center', 'left', 'right' ) : array( '', 'space-between', 'center', 'left', 'right' );
		if ( array_key_exists( $alignment, $attrs ) && ! in_array( $attrs[ $alignment ], $allowed, true ) ) {
			return false;
		}
	}
	foreach ( array( 'progressColor', 'barBackground' ) as $color ) {
		if ( array_key_exists( $color, $attrs ) && ( ! is_string( $attrs[ $color ] ) || 1 !== preg_match( '/^#[a-f0-9]{6}$/Di', $attrs[ $color ] ) ) ) {
			return false;
		}
	}
	foreach ( array( 'displayLabel', 'displayPercent', 'delayUntilInView' ) as $boolean ) {
		if ( array_key_exists( $boolean, $attrs ) && ! is_bool( $attrs[ $boolean ] ) ) {
			return false;
		}
	}
	foreach ( array( 'label', 'ariaLabel' ) as $text ) {
		if ( array_key_exists( $text, $attrs ) && ( ! is_string( $attrs[ $text ] ) || 1 !== preg_match( '/\A[^\x00-\x1F\x7F<>{}]{0,200}\z/uD', $attrs[ $text ] ) ) ) {
			return false;
		}
	}
	foreach ( array( 'numberPrefix', 'numberSuffix' ) as $text ) {
		if ( array_key_exists( $text, $attrs ) && ( ! is_string( $attrs[ $text ] ) || 1 !== preg_match( '/\A[^\x00-\x1F\x7F<>{}]{0,80}\z/uD', $attrs[ $text ] ) ) ) {
			return false;
		}
	}
	if ( array_key_exists( 'labelPosition', $attrs ) && ! in_array( $attrs['labelPosition'], array( 'above', 'inside', 'below' ), true ) ) {
		return false;
	}
	if ( array_key_exists( 'decimal', $attrs ) && ! in_array( $attrs['decimal'], array( 'none', 'one', 'two' ), true ) ) {
		return false;
	}
	$display_label   = $attrs['displayLabel'] ?? true;
	$display_percent = $attrs['displayPercent'] ?? true;
	if ( ! empty( $attrs['label'] ) && true !== $display_label ) {
		return false;
	}
	if ( ( ! empty( $attrs['numberPrefix'] ) || ! empty( $attrs['numberSuffix'] ) || ( isset( $attrs['decimal'] ) && 'none' !== $attrs['decimal'] ) ) && true !== $display_percent ) {
		return false;
	}
	return 'kadence/progress-bar' === $name
		&& $type instanceof WP_Block_Type
		&& is_array( $callback ) && 2 === count( $callback )
		&& isset( $callback[0] ) && $callback[0] instanceof Kadence_Blocks_Progress_Bar_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 )
		&& 'line' === ( $attrs['barType'] ?? null )
		&& is_bool( $display_label )
		&& is_bool( $display_percent )
		&& false === ( $attrs['delayUntilInView'] ?? null )
		&& isset( $block['innerBlocks'] ) && array() === $block['innerBlocks']
		&& '' === ( $block['innerHTML'] ?? null )
		&& isset( $block['innerContent'] ) && array() === $block['innerContent'];
}
