<?php
/**
 * Server-side render for blocksify/accordion (parent).
 *
 * Each Accordion Item renders itself via WP's normal recursive content-build
 * (see `accordion-item/render.php`). This parent's job is to:
 *   1. Wrap the rendered `$content` with the per-instance class +
 *      Interactivity API namespace + state context.
 *   2. Emit `data-indicator-type` / `data-indicator-position` attrs so
 *      style.css + view.js can read indicator config without per-instance
 *      CSS.
 *
 * NO `pre_render_block` filter / no manual children-walk — `$content`
 * already contains the recursively-rendered Accordion Item DOM. Different
 * from Tabs (which needs to short-circuit Tab rendering to prevent
 * double-render); accordion is a list, each item self-contained.
 *
 * Per-instance CSS comes from `Blocksify_CSS_Pipeline` (single-handle pool
 * at `wp_enqueue_scripts`) + `Blocksify_CSS_Generator::emit_accordion_local_rules`
 * for the indicator + gap rules.
 *
 * @package Blocksify
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Inner blocks rendered (each Accordion Item).
 * @var WP_Block $block      Parsed block instance.
 */

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

if ( isset( $_GET['blocksify_debug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$debug_css = Blocksify_CSS_Generator::generate_block_css( 'blocksify/accordion', $attributes );
	echo "\n<!-- Blocksify Accordion CSS:\n" . esc_html( $debug_css ) . "\n-->\n";
}

$uid                = isset( $attributes['uid'] ) ? preg_replace( '/[^a-z0-9_-]/i', '', (string) $attributes['uid'] ) : '';
$allow_multiple     = ! isset( $attributes['allowMultiple'] ) || true === $attributes['allowMultiple'];
$default_open_item  = isset( $attributes['defaultOpenItem'] ) && is_numeric( $attributes['defaultOpenItem'] )
	? (int) $attributes['defaultOpenItem']
	: null;
$indicator_type     = isset( $attributes['indicatorType'] ) ? (string) $attributes['indicatorType'] : 'plus-minus';
$indicator_position = isset( $attributes['indicatorPosition'] ) ? (string) $attributes['indicatorPosition'] : 'right';
$icon_position      = isset( $attributes['iconPosition'] ) ? (string) $attributes['iconPosition'] : '';
$title_display      = isset( $attributes['titleDisplay'] ) ? (string) $attributes['titleDisplay'] : 'wrapped';
$title_display_tab  = isset( $attributes['titleDisplayTablet'] ) ? (string) $attributes['titleDisplayTablet'] : '';
$title_display_mob  = isset( $attributes['titleDisplayMobile'] ) ? (string) $attributes['titleDisplayMobile'] : '';
$user_class         = isset( $attributes['className'] ) ? trim( (string) $attributes['className'] ) : '';
$anchor             = isset( $attributes['anchor'] ) ? (string) $attributes['anchor'] : '';

$allowed_indicators = array( 'plus-minus', 'chevron', 'arrow', 'none' );
$indicator_type     = in_array( $indicator_type, $allowed_indicators, true ) ? $indicator_type : 'plus-minus';
$allowed_positions  = array( 'left', 'right' );
$indicator_position = in_array( $indicator_position, $allowed_positions, true ) ? $indicator_position : 'right';
$allowed_icon_pos   = array( 'left', 'right' );
$icon_position      = in_array( $icon_position, $allowed_icon_pos, true ) ? $icon_position : '';
$allowed_displays   = array( 'wrapped', 'truncated' );
$title_display      = in_array( $title_display, $allowed_displays, true ) ? $title_display : 'wrapped';
// Per-device overrides — empty string means "no override", anything else
// must validate against the same enum or fall back to '' (no override
// emitted, desktop value cascades through @media as-is).
$title_display_tab  = in_array( $title_display_tab, $allowed_displays, true ) ? $title_display_tab : '';
$title_display_mob  = in_array( $title_display_mob, $allowed_displays, true ) ? $title_display_mob : '';

$classes = array( 'is-layout-stacked' );
if ( '' !== $uid ) {
	$classes[] = 'bsy-accordion-' . $uid;
}
if ( '' !== $user_class ) {
	$classes[] = $user_class;
}
$class_attr = esc_attr( implode( ' ', $classes ) );

// In single-expand mode, `defaultOpenItem` is the persisted "which
// item starts open for visitors" intent. The Interactivity API store
// mutates `ctx.currentItem` at runtime when users click, but those
// mutations stay in the IA context object (per-tab, per-session) and
// never write back to post attributes — there's no `currentItem`
// attribute on this block. Editor-only UI state lives in
// the `blocksify/accordion-ui` data store keyed by accordion clientId
// (see `src/shared/stores/accordion-ui.js`); frontend-render.php is
// unaffected by editor-only state.
$initial_current = null;
if ( ! $allow_multiple && null !== $default_open_item ) {
	$initial_current = $default_open_item;
}

$context = array(
	'allowMultiple' => (bool) $allow_multiple,
	'currentItem'   => $initial_current,
);
$context_json = wp_json_encode( $context );
if ( false === $context_json ) {
	$context_json = '{}';
}

$attrs_html  = 'class="' . $class_attr . '"';
$attrs_html .= ' data-wp-interactive="blocksify/accordion"';
$attrs_html .= ' data-wp-context=\'' . esc_attr( $context_json ) . '\'';
$attrs_html .= ' data-indicator-type="' . esc_attr( $indicator_type ) . '"';
$attrs_html .= ' data-indicator-position="' . esc_attr( $indicator_position ) . '"';
if ( '' !== $icon_position ) {
	$attrs_html .= ' data-icon-position="' . esc_attr( $icon_position ) . '"';
}
$attrs_html .= ' data-title-display="' . esc_attr( $title_display ) . '"';
if ( '' !== $title_display_tab ) {
	$attrs_html .= ' data-title-display-tablet="' . esc_attr( $title_display_tab ) . '"';
}
if ( '' !== $title_display_mob ) {
	$attrs_html .= ' data-title-display-mobile="' . esc_attr( $title_display_mob ) . '"';
}
if ( '' !== $anchor ) {
	$attrs_html .= ' id="' . esc_attr( $anchor ) . '"';
}

echo '<div ' . $attrs_html . '>' . $content . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
