<?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  Gutenberg\Blocks
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		6.2.0
 * @version		6.5.7
 */

namespace WeCodeArt\Gutenberg\Blocks\Text;

defined( 'ABSPATH' ) || exit;

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

/**
 * Gutenberg details block.
 */
class Details extends Dynamic {

	use Singleton;

	/**
	 * Block namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'core';

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'details';

	/**
	 * Block args.
	 *
	 * @param	array $current	Existing register args
	 *
	 * @return 	array
	 */
	public function block_type_args( array $current ): array {
		$supports 	= get_prop( $current, [ 'supports' ], [] );

		return [
			'render_callback' 	=> self::has_seo_support() ? [ $this, 'render' ] : null,
			'supports'			=> wp_parse_args( [
				'anchor'	=> true
			], $supports )
		];
	}

	/**
	 * Dynamically renders the `core/table` block.
	 *
	 * @param 	array 	$attributes	The attributes.
	 * @param 	string 	$content 	The block markup.
	 *
	 * @return 	string 	The block markup.
	 */
	public function render( array $attributes = [], string $content = '' ): string {	
		$dom 		= wecodeart( 'dom' )::create( $content );
		$details	= wecodeart( 'dom' )::get_element( 'details', $dom, 0 );
		$has_id 	= is_object( $details ) ? $details->getAttribute( 'id' ) : '';

		if( empty( $has_id ) ) {
			$details->setAttribute( 'id', self::generate_id( [ 'innerHTML' => $content ] ) );
		}

		return $dom->saveHtml();
	}

	/**
	 * Check if has SEO support
	 *
	 * @return boolean
	 */
	public static function has_seo_support(): bool {
		if( wecodeart_if( 'is_rankmath_active' ) || wecodeart_if( 'is_yoast_active' ) ) {
			return \apply_filters( 'wecodeart/filter/gutenberg/block/details/seo', true );
		}

		return false;
	}

	/**
	 * Generate ID
	 *
	 * @param	array	$block
	 *
	 * @return 	string
	 */
	public static function generate_id( array $block ): string {
		return 'faq-' . \esc_attr( md5( self::get_question( $block ) ) );
	}

	/**
	 * Get Question
	 *
	 * @param	array	$block
	 *
	 * @return 	string
	 */
	public static function get_question( array $block, $tag = 'summary' ): string {
		$dom 		= wecodeart( 'dom' )::create( get_prop( $block, [ 'innerHTML' ], '' ) );
		$summary	= wecodeart( 'dom' )::get_element( $tag, $dom, 0 );
		
		return is_object( $summary ) ? $summary->nodeValue : '';
	}

	/**
	 * Get Answer
	 *
	 * @param	array	$blocks
	 *
	 * @return 	string
	 */
	public static function get_answer( array $block = [] ): string {
		$content = '';

		foreach( get_prop( $block, [ 'innerBlocks' ], [] ) as $block ) {
			if( ! in_array( $block['blockName'], [ 'core/heading', 'core/paragraph' ], true ) ) {
				continue;
			}

			if( ! empty( $inner = get_prop( $block, [ 'innerBlocks' ], [] ) ) ) {
				$content .= self::get_question_content( $inner );
			}

			$content .= ' ' . strip_tags( $block['innerHTML'], [
				'h1',
				'h2',
				'h3',
				'h4',
				'h5',
				'h6',
				'p',
				'strong',
				'b',
				'em',
				'br',
				'a',
				'i',
			] );
		}

		return trim( preg_replace( '/(<[^>]+) style=".*?"/i', '$1', $content ) );
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles(): string {
		return <<<CSS
			.wp-block-details {
				--wp--details--padding-y: 1rem;
				--wp--details--padding-x: 2rem;
				--wp--details--padding: var(--wp--details--padding-y) var(--wp--details--padding-x);
				--wp--details--chevron-size: .4rem;
				--wp--details--chevron-color: currentColor;
				--wp--details--chevron-transition: .25s transform ease;
				--wp--details--border: 1px solid var(--wp--preset--color--accent);
				border: var(--wp--details--border);
			}
			.wp-block-details details + details {
				border-top: var(--wp--details--border);
			}
			.wp-block-details summary {
				position: relative;
				display: block;
				padding: var(--wp--details--padding);
				font-size: var(--wp--preset--font-size--normal);
				font-weight: 700;
				transition: background-color .5s ease-in-out;
				cursor: pointer;
			}
			.wp-block-details summary::before {
				content: "";
				position: absolute;
				top: calc(50% - var(--wp--details--chevron-size));
				left: 1rem;
				border-width: var(--wp--details--chevron-size);
				border-style: solid;
				border-color: transparent transparent transparent var(--wp--details--chevron-color);
				transform: rotate(0);
				transform-origin: .125em 50%;
				transition: var(--wp--details--chevron-transition);
			}
			.wp-block-details summary:hover,
			.wp-block-details summary:focus,
			details[open].wp-block-details summary,
			.wp-block-details details[open] summary {
				background-color: var(--wp--preset--color--accent);
			}
			details[open].wp-block-details summary,
			.wp-block-details details[open] summary {
				border-bottom: var(--wp--details--border);
			}
			details[open].wp-block-details,
			.wp-block-details details[open] {
				padding-bottom: var(--wp--details--padding-y);
			}
			details[open].wp-block-details > summary::before,
			.wp-block-details details[open] > summary::before {
				transform: rotate(90deg);
			}
			details.wp-block-details > *:not(summary),
			.wp-block-details details > *:not(summary) {
				margin: var(--wp--details--padding);
			}
			details summary::marker,
			details summary::-webkit-details-marker {
				display: none;
				content: none;
			}
		CSS;
	}
}