<?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.1
 */

namespace WeCodeArt\Gutenberg\Blocks\Post;

defined( 'ABSPATH' ) || exit;

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

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

	use Singleton;

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

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

	/**
	 * Init.
	 */
	public function init() {
		\add_filter( 'post_class', [ $this, 'post_classes' ] );
	}

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

		return [
			'render_callback' => [ $this, 'render' ],
			'supports' => wp_parse_args( [
				'spacing' => [
					'padding'	=> true,
					'margin'	=> true,
					'blockGap' 	=> true
				],
			], $supports )
		];
	}

	/**
	 * Dynamically renders the `core/post-template` block.
	 *
	 * @param 	array 	$attributes	The attributes.
	 * @param 	string 	$content 	The block markup.
	 * @param 	object 	$block 		The block data.
	 *
	 * @return 	string 	The block markup.
	 */
	public function render( array $attributes = [], string $content = '', object $block = null ): string {
		$columns 	= get_prop( $attributes, [ 'layout', 'columnCount' ], get_prop( $block->context, [ 'displayLayout', 'columns' ], 3 ) );
		$callback 	= function( array $classes = [] ) use( $columns ) {
			$grid_cl = [ 'span-12', 'span-md-6', 'span-lg-' . ( 12 / $columns ) ];

			wecodeart( 'styles' )->Utilities->load( $grid_cl ); 

			return array_merge( $classes, $grid_cl );
		};

		\add_filter( 'post_class', $callback );
		$content	= render_block_core_post_template( $attributes, $content, $block );
		\remove_filter( 'post_class', $callback );

		$content	= wecodeart( 'dom' )::processor( $content );
		
		if( $content->next_tag( [ 'class_name' => 'wp-block-post-template' ] ) ) {
			if( get_prop( $attributes, [ 'layout', 'type' ], get_prop( $block->context, [ 'displayLayout', 'type' ], '' ) ) === 'grid' ) {
				$content->add_class( 'grid' );
				$content->remove_class( 'columns-' . $columns );
			}
		}

		return (string) $content;
	}

	/**
	 * Filter classes to the array of post classes.
	 *
	 * @param 	array $classes Classes for the post.
	 * 
	 * @return 	array
	 */
	public function post_classes( $classes ) {
		if ( is_admin() ) {
			return $classes;
		}
		
		// Add 'entry to the post class array.
		$classes[] = 'entry';

		// Remove 'hentry' from post class array.
		$classes = array_diff( $classes, [ 'hentry' ] );
		
		return $classes;
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
		return <<<CSS
			.wp-block-post-template {
				list-style: none;
				padding: 0;
			}
			.wp-block-post-template.wp-block-post-template.has-background {
				padding: var(--wp--preset--spacing--g);
			}
		CSS;
	}
}
