<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variables are scoped when required inside the block render callback.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once dirname( __DIR__, 3 ) . '/includes/block-css-generator.php';
require_once dirname( __DIR__, 3 ) . '/includes/render-helpers/list-css.php';

/**
 * Render callback for the AxcelersBlocks List block.
 *
 * Generic main/hover CSS for the <ul>/<ol> element comes from the shared
 * generator. The list-specific marker and icon CSS (custom selectors the shared
 * generator cannot express) comes from axcelersblocks_list_generate_marker_css().
 *
 * @package AxcelersBlocks
 */

// ─── Tag & identity ───────────────────────────────────────────────────────────

$tag_name = isset( $attributes['tagName'] ) && in_array( $attributes['tagName'], [ 'ul', 'ol' ], true )
	? tag_escape( $attributes['tagName'] )
	: 'ul';
$start                  = isset( $attributes['start'] ) ? absint( $attributes['start'] ) : 1;
$reversed               = ! empty( $attributes['reversed'] );
$anchor                 = isset( $attributes['anchor'] ) ? sanitize_title( $attributes['anchor'] ) : '';
$unique_class           = isset( $attributes['uniqueClass'] ) ? sanitize_html_class( $attributes['uniqueClass'] ) : '';
$list_has_custom_marker = ! empty( $attributes['iconSvg'] ) && is_string( $attributes['iconSvg'] ) && false !== strpos( $attributes['iconSvg'], '<svg' );
if ( empty( $unique_class ) ) {
	$unique_class = sanitize_html_class( 'axb-' . wp_unique_id() );
}

// ─── Wrapper attributes ───────────────────────────────────────────────────────
// Built manually (not get_block_wrapper_attributes) to keep <ul>/<ol> clean of
// WP's has-global-padding/is-layout-constrained/wp-block-* helper classes and to
// support the native start/reversed attributes.

$extra_attrs = [
	'class' => trim( 'axcelersblocks-list ' . $unique_class . ( $list_has_custom_marker ? ' has-custom-marker' : '' ) ),
];
if ( $anchor ) {
	$extra_attrs['id'] = $anchor;
}
if ( 'ol' === $tag_name && $start > 1 ) {
	$extra_attrs['start'] = (string) $start;
}
if ( 'ol' === $tag_name && $reversed ) {
	$extra_attrs['reversed'] = 'reversed';
}
if ( ! empty( $attributes['align'] ) ) {
	$align = sanitize_key( $attributes['align'] );
	if ( in_array( $align, [ 'wide', 'full', 'left', 'right', 'center' ], true ) ) {
		$extra_attrs['class'] .= ' align' . $align;
	}
}

$attr_pairs = [];
foreach ( $extra_attrs as $key => $value ) {
	if ( '' === $value || null === $value ) {
		continue;
	}
	$attr_pairs[] = sprintf( '%s="%s"', esc_attr( $key ), esc_attr( (string) $value ) );
}
$wrapper_attrs = implode( ' ', $attr_pairs );

// ─── CSS generation & collection ──────────────────────────────────────────────
// has_icon is disabled on the shared generator because the list owns its own
// .icon / marker selectors (produced by the marker helper, not the shared .icon).

$devices    = axcelersblocks_get_responsive_devices();
$styles_css = axcelersblocks_generate_block_styles( $unique_class, $devices, $attributes, [ 'has_icon' => false ] );
$hover_css  = axcelersblocks_generate_block_styles_hover( $unique_class, $devices, $attributes, [ 'has_icon' => false ] );
$marker_css = axcelersblocks_list_generate_marker_css( $unique_class, $devices, $attributes );
$styles_css = array_merge( $styles_css, $hover_css, $marker_css );

if ( ! empty( $attributes['listStyleType'] ) ) {
	$safe_list_style_types = [ 'none', 'disc', 'circle', 'square', 'decimal', 'decimal-leading-zero', 'lower-alpha', 'upper-alpha', 'lower-roman', 'upper-roman' ];
	$list_style_type       = sanitize_key( $attributes['listStyleType'] );
	if ( in_array( $list_style_type, $safe_list_style_types, true ) ) {
		$styles_css[] = '.' . $unique_class . ' { list-style-type: ' . $list_style_type . '; }';
	}
}

axcelersblocks_collect_block_styles( $unique_class, $block, $styles_css );

// ─── Output ───────────────────────────────────────────────────────────────────

printf(
	'<%1$s %2$s>%3$s</%1$s>',
	esc_attr( $tag_name ),
	$wrapper_attrs, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- attribute values are individually escaped above.
	wp_kses( $content, axcelersblocks_get_allowed_svg_html() ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_kses sanitizes.
);
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
