<?php
/**
 * Accordion template.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

if ( empty( $items ) ) {
	return;
}

$accordion_classes = array(
	'rsmspark-cw',
	'rsmspark-cw-accordion',
);
if ( ! empty( $faq_mode ) ) {
	$accordion_classes[] = 'rsmspark-cw-accordion--faq';
}

$first_panel_open = true;
?>
<div id="<?php echo esc_attr( $widget_uid ); ?>" class="<?php echo esc_attr( implode( ' ', $accordion_classes ) ); ?>" style="<?php echo esc_attr( $style_vars ); ?>" data-rsmspark-cw-accordion>
	<?php foreach ( $items as $item ) : ?>
		<?php
		$is_category = ( 'category' === ( $item['item_type'] ?? 'item' ) );
		if ( $is_category ) :
			?>
			<div class="rsmspark-cw-accordion__category" role="presentation">
				<?php echo esc_html( $item['title'] ); ?>
			</div>
			<?php
			continue;
		endif;
		$is_open = $first_panel_open;
		if ( $first_panel_open ) {
			$first_panel_open = false;
		}
		?>
		<div class="rsmspark-cw-accordion__item" data-rsmspark-cw-accordion-slug="<?php echo esc_attr( $item['slug'] ?? '' ); ?>">
			<button
				type="button"
				class="rsmspark-cw-accordion__trigger"
				aria-expanded="<?php echo $is_open ? 'true' : 'false'; ?>"
				data-rsmspark-cw-accordion-trigger
			>
				<span><?php echo esc_html( $item['title'] ); ?></span>
			</button>
			<div class="rsmspark-cw-accordion__panel rsmspark-rt-root<?php echo $is_open ? ' is-open' : ''; ?>" <?php echo ! $is_open ? 'hidden' : ''; ?>>
				<?php echo wp_kses( Rsmspark_Rich_Text_Body::render( $item['content'] ), Rsmspark_Rich_Text_Body::allowed_html() ); ?>
			</div>
		</div>
	<?php endforeach; ?>
</div>
<?php if ( ! empty( $faq_mode ) ) : ?>
	<?php Rsmspark_Content_Widgets_Schema::print_faq_page_json_ld( $items ); ?>
<?php endif; ?>
