<?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.0.0
 * @version		6.7.0
 */

namespace WeCodeArt\Gutenberg\Blocks\Site;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Singleton;
use WeCodeArt\Gutenberg\Blocks\Dynamic;
use function WeCodeArt\Functions\{ get_prop, toJSON };
use function add_filter;

/**
 * Gutenberg Template Part block.
 */
class Template extends Dynamic {

	use Singleton;

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

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'template-part';

	/**
	 * Init.
	 */
	public function init() {
		\add_filter( 'render_block_' . $this->get_block_type(),	[ $this, 'filter_render' ], 20, 2 );
    }

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

		return [
			'supports'	=> wp_parse_args( [
				'color'		=> true,
				'position'	=> true,
				'layout'	=> true,
				// Allow renaming, used on collapse toggle (not on admin)
				'renaming' 	=> true,
				'spacing'	=> [
					'margin'  	=> true,
					'padding' 	=> true,
					'blockGap' 	=> true,
					'__experimentalDefaultControls' => [
						'margin' 	=> false,
						'padding' 	=> false,
						'blockGap'	=> true,
					]
				]
			], $supports ),
			'styles'	=> wp_parse_args( [
				[
					'name' 	=> 'default',
					'label' => __( 'Default', 'wecodeart' ),
					'isDefault' => true
				],
				[
					'name' 	=> 'collapse',
					'label' => __( 'Collapse', 'wecodeart' ),
				]
			], $styles )
		];
	}

    /**
	 * Dynamically renders the `core/template-part` block.
	 *
	 * @param 	string 	$content 	The block markup.
	 *
	 * @return 	string 	The block markup.
	 */
	public function filter_render( string $content, array $block ): string {
		$attributes = get_prop( $block, [ 'attrs' ], [] );
		$tagName 	= strtoupper( get_prop( $attributes, [ 'tagName' ], '' ) );
		
		$markup		= wecodeart( 'dom' )::processor( $content );

		if ( $markup->next_tag( [
			'tag_name' => $tagName
		]) ) {
			switch( $tagName ) :
				case 'HEADER' :
					$markup->set_attribute( 'itemscope', 'itemscope' );
					$markup->set_attribute( 'itemtype', 'https://schema.org/WPHeader' );
				break;
				case 'FOOTER' :
					$markup->set_attribute( 'itemscope', 'itemscope' );
					$markup->set_attribute( 'itemtype', 'https://schema.org/WPFooter' );
				break;
				case 'ASIDE' :
					$markup->set_attribute( 'itemscope', 'itemscope' );
					$markup->set_attribute( 'itemtype', 'https://schema.org/WPSideBar' );
				break;
			endswitch;
		}

		$content = $markup->get_updated_html();

		if( str_contains( get_prop( $attributes, [ 'className' ], '' ), 'is-style-collapse' ) ) {
			$content = wecodeart( 'blocks' )->get( 'core/group' )::create_collapse( $attributes, $content ); 
		}
		
		return $content;
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
		return '';
	}
}
