<?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		5.0.0
 * @version		6.4.2
 */

namespace WeCodeArt\Gutenberg\Blocks\Text;

defined( 'ABSPATH' ) || exit;

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

/**
 * Gutenberg Quote block.
 */
class Quote extends Dynamic {

	use Singleton;

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

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

	/**
	 * 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' 	=> [ $this, 'render' ],
			'supports' 			=> wp_parse_args( [
				'__experimentalBorder'	=> [
					'radius'                        => true,
					'width'                         => true,
					'color'                         => true,
					'style'                         => true,
					'__experimentalDefaultControls' => [
						'width' => true,
						'color' => true,
					],
				],
				'spacing'	=> [
					'margin'  	=> true,
					'padding' 	=> true,
				],
				'shadow'	=> true
			], $supports )
		];
	}

	/**
	 * Dynamically renders the `core/quote` 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 = $this->dom( $content );
		
		// Quote Changes
		$quote	= $dom->getElementsByTagName( 'blockquote' )->item(0);
		if( $quote ) {
			$figure	= wecodeart( 'dom' )::create_element( 'figure', $dom );
			$figure->setAttribute( 'class', $quote->getAttribute( 'class' ) );
			$quote->setAttribute( 'class', 'blockquote' );
			$quote->parentNode->replaceChild( $figure, $quote );
			$figure->appendChild( $quote );
		}

		// Cite Changes
		$cite	= $dom->getElementsByTagName( 'cite' )->item(0);
		if( $cite ) {
			$caption = wecodeart( 'dom' )::create_element( 'figcaption', $dom );
			$caption->setAttribute( 'class', 'blockquote-footer' );
			$cite->parentNode->replaceChild( $caption, $cite );
			$caption->appendChild( $cite );
			$figure->appendChild( $caption );
		}

		return $dom->saveHTML();
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles(): string {
		return <<<CSS
			:where(.wp-block-quote) {
				margin: 0;
			}
			.wp-block-quote:not(.is-style-plain) {
				border-left: 4px solid var( --wp--preset--color--primary );
				padding-left: 1rem;
			}
			.wp-block-quote.has-background {
				padding: 1rem;
				border-left-color: rgba(0,0,0,.05);
			}
			.wp-block-quote .blockquote {
				margin: 0;
				font-size: inherit;
			}
			.wp-block-quote .blockquote-footer {
				font-size: var(--wp--preset--font-size--small);
				text-align: left;
				color: var(--wp--preset--color--dark);
			}
			.wp-block-quote .blockquote-footer::before {
				content: "— ";
			}
		CSS;
	}
}
