<?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.2.2
 * @version		6.0.0
 */

namespace WeCodeArt\Gutenberg\Blocks\Comment;

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 = 'comment-template';

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

	/**
	 * Dynamically renders the `core/comment-template` block.
	 *
	 * @param 	array 	$attributes	The attributes.
	 * @param 	string 	$content 	The block markup.
	 * @param 	object 	$block 		The block object.
	 *
	 * @return 	string 	The block markup.
	 */
	public function render( array $attributes = [], string $content = '', object $block = null ): string {
		$post_id = $block->context['postId'];

        // Bail out early if the post ID is not set for some reason.
        if ( ! isset( $post_id ) ) {
            return '';
        }

		if( post_password_required( $post_id ) ) {
			return wecodeart( 'dom' )::wrap( 'wp-block-comments-protected', [ [
				'tag' 	=> 'p',
			] ], 'printf', [ 
				esc_html__( 'This post is password protected. Enter the password to view comments or leave a comment.', 'wecodeart' )
			], false );
		}

        // Get what we need about comments for the current post.
		$comments_query		= new \WP_Comment_Query( build_comment_query_vars_from_block( $block ) );
        $comments 			= $comments_query->get_comments();
		$comment_order 		= get_option( 'comment_order' );

		if ( 'desc' === $comment_order ) {
			$comments = array_reverse( $comments );
		}

		// List
        $content .= wecodeart( 'dom' )::wrap( 'wp-block-comment-template', [
			[
				'tag' 	=> 'ul',
				'attrs'	=> $this->get_block_wrapper_attributes( [
					'class' => 'list-unstyled'
				] )
			]
		], [ __CLASS__, 'render_comments' ], [ $comments, $block ], false );

		return $content;
	}

	/**
	 * Render Threaded Comments
	 *
	 * @since	5.3.3
	 * @version	5.7.0
	 *
	 * @return 	string
	 */
	public static function render_comments( $comments, $block ) {
		global $comment_depth;
		$thread_comments       = get_option( 'thread_comments' );
		$thread_comments_depth = get_option( 'thread_comments_depth' );

		if ( empty( $comment_depth ) ) {
			$comment_depth = 1;
		}
		
		$content = '';

		foreach ( $comments as $comment ) {

			$block_instance = $block->parsed_block;
			$block_instance['blockName'] = 'core/null';
	
			$block_content = ( new \WP_Block( $block_instance, [
				'commentId' => $comment->comment_ID,
			] ) )->render( [ 'dynamic' => false ] );
	
			$children = $comment->get_children();
	
			// If the comment has children, recurse to create the HTML for the nested comments.
			if ( ! empty( $children ) && ! empty( $thread_comments ) ) {

				// Load Utilities
				wecodeart( 'styles' )->Utilities->load( [ 'ps-3', 'ps-md-5', 'mt-5' ] );

				if ( $comment_depth < $thread_comments_depth ) {
					++$comment_depth;
					$block_content .= wecodeart( 'dom' )::wrap( 'wp-block-comment-template-children', [
						[
							'tag' 	=> 'ol',
							'attrs'	=> [
								'class' => 'comment__children list-unstyled ps-3 ps-md-5 mt-5'
							]
						]
					], [ __CLASS__, 'render_comments' ], [ $children, $block ], false );
					--$comment_depth;
				} else {
					$block_content .= [ __CLASS__, 'render_comments' ];
				}
			}
	
			$content .= wecodeart( 'dom' )::wrap( 'wp-block-comment', [
				[
					'tag' 	=> 'li',
					'attrs'	=> [
						'id'	=> 'comment-' . $comment->comment_ID,
						'class' => implode( ' ', get_comment_class( 'mb-5', $comment ) )
					]
				]
			], $block_content, [], false );
		}
	
		// This is ok, we render gutenberg blocks here.
		echo $content;
	}

	/**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
		return <<<CSS
			.wp-block-comments-query-loop:empty {
				display: none;
			}
		CSS;
	}
}
