<?php
/**
 * Shared render helpers for the table block family.
 *
 * @package AxcelersBlocks
 */

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

require_once __DIR__ . '/block-css-generator.php';


if ( ! function_exists( 'axcelersblocks_table_collect_styles' ) ) {
	/**
	 * Collect generated CSS rules using the plugin's shared inline CSS collector.
	 *
	 * @param string   $block_name Block name.
	 * @param string[] $styles     CSS rules.
	 * @return void
	 */
	function axcelersblocks_table_collect_styles( $block_name, $styles ) {
		if ( empty( $styles ) || ! is_array( $styles ) ) {
			return;
		}

		foreach ( $styles as $style_rule ) {
			axcelersblocks_collect_inline_style_rule( $block_name, $style_rule );
		}
	}
}

if ( ! function_exists( 'axcelersblocks_table_build_attrs' ) ) {
	/**
	 * Build an HTML attribute string.
	 *
	 * @param array<string, string|array<string>> $extra_attrs Attributes.
	 * @return string
	 */
	function axcelersblocks_table_build_attrs( $extra_attrs ) {
		$attr_pairs = array();

		foreach ( $extra_attrs as $key => $value ) {
			if ( 'class' === $key ) {
				$value = is_array( $value ) ? implode( ' ', array_filter( $value ) ) : $value;
			}

			if ( '' === $value || null === $value ) {
				continue;
			}

			$attr_pairs[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( (string) $value ) );
		}

		return implode( ' ', $attr_pairs );
	}
}

if ( ! function_exists( 'axcelersblocks_table_render_element' ) ) {
	/**
	 * Render a semantic table element block and collect its inline styles.
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content    Inner content.
	 * @param string $block_name Block name for style collection.
	 * @param array  $config     Element configuration.
	 * @return string
	 */
	function axcelersblocks_table_render_element( $attributes, $content, $block_name, $config ) {
		$allowed_tags  = $config['allowed_tags'] ?? array();
		$default_tag   = $config['default_tag'] ?? 'div';
		$base_classes  = $config['base_classes'] ?? array();
		$fallback_slug = $config['fallback_slug'] ?? 'axb-table-element';

		$tag_name = isset( $attributes['tagName'] ) && in_array( $attributes['tagName'], $allowed_tags, true )
			? $attributes['tagName']
			: $default_tag;

		$anchor = isset( $attributes['anchor'] ) ? sanitize_title( $attributes['anchor'] ) : '';
		$unique_class = isset( $attributes['uniqueClass'] ) ? sanitize_html_class( $attributes['uniqueClass'] ) : '';

		if ( empty( $unique_class ) ) {
			$unique_class = sanitize_html_class( $fallback_slug . '-' . wp_unique_id() );
		}

		// Generic main + hover CSS for this element's own .uniqueClass, via the shared
		// block CSS generator (same engine every other block uses). Table elements have
		// no icon/link selectors, so those are disabled.
		$attrs   = is_array( $attributes ) ? $attributes : array();
		$devices = axcelersblocks_get_responsive_devices();
		$styles  = array_merge(
			axcelersblocks_generate_block_styles( $unique_class, $devices, $attrs, array( 'has_icon' => false, 'is_linked' => false ) ),
			axcelersblocks_generate_block_styles_hover( $unique_class, $devices, $attrs, array( 'has_icon' => false, 'is_linked' => false ) )
		);

		axcelersblocks_table_collect_styles( $block_name, $styles );

		$extra_attrs = array(
			'class' => array_merge( (array) $base_classes, array( $unique_class ) ),
		);

		if ( $anchor ) {
			$extra_attrs['id'] = $anchor;
		}

		// Extra raw HTML attributes (e.g. colspan/rowspan for merged cells).
		if ( ! empty( $config['extra_attrs'] ) && is_array( $config['extra_attrs'] ) ) {
			$extra_attrs = array_merge( $extra_attrs, $config['extra_attrs'] );
		}

		return sprintf(
			'<%1$s %2$s>%3$s</%1$s>',
			esc_html( $tag_name ),
			axcelersblocks_table_build_attrs( $extra_attrs ),
			$content
		);
	}
}
