<?php
/**
 * WeCodeArt Framework
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package		WeCodeArt Framework
 * @subpackage  RankMath\Blocks
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		6.1.2
 * @version		6.4.2
 */

namespace WeCodeArt\Support\Plugins\RankMath\Blocks;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Config\Traits\Singleton;
use WeCodeArt\Gutenberg\Blocks\Dynamic;
use function WeCodeArt\Functions\get_prop;

/**
 * Gutenberg RankMath FAQ block.
 */
class Faq extends Dynamic {

	use Singleton;

	/**
	 * Block namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'rank-math';

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'faq-block';

	/**
	 * Block args.
	 *
	 * @return 	array
	 */
	public function block_type_args(): array {
		return [
			'render_callback'  	=> [ $this, 'render' ],
			'style'				=> [ $this->get_asset_handle(), 'wp-block-details' ]
		];
	}

	/**
	 * Get block asset handle.
	 *
	 * @return string
	 */
	public function get_asset_handle(): string {
		return $this->namespace . '-' . $this->block_name;
	}

    /**
	 * Filter FAQ
	 * 
	 * @param 	string 	$attributes
	 * @param 	array 	$content
	 * 
	 * @return 	string
	 */
	public function render( array $attributes = [], string $content = '' ): string {
		$condition = ( get_prop( $attributes, [ 'listStyle' ] ) === '' && get_prop( $attributes, [ 'titleWrapper' ] ) === 'div' );
		
		if( $condition ) { // Apply only for default styleing with div titles (so we respect block settings).
			$dom		= wecodeart( 'dom' )::create( $content );
			$sections	= wecodeart( 'dom' )::get_elements_by_class( $dom, 'rank-math-faq-item', 'div' );
			$summaries	= wecodeart( 'dom' )::get_elements_by_class( $dom, 'rank-math-question', 'div' );

			if( count( $sections ) ) {
				array_walk( $sections, static fn( $section ) => wecodeart( 'dom' )::change_tag( $section, 'details' ) );
				array_walk( $summaries, static fn( $summary ) => wecodeart( 'dom' )::change_tag( $summary, 'summary' ) );
			}

			$content = wecodeart( 'dom' )::processor( $dom->saveHtml() );
			$content->next_tag();
			$content->add_class( 'wp-block-details' );
		}

		return (string) $content;
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
		return <<<CSS
			.wp-block-rank-math-faq-block details[open] {
				padding-bottom: 0;
			}
		CSS;
	}
}
