<?php
/**
 * Template part for displaying content of image (attachment)
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#attachment
 *
 * @package errigal
 * @since 1.0.0
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Content_Hook as Content_Hook,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Template_Part as Template_Part,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

while ( have_posts() ) {
	the_post();
	get_template_part(
		'template-parts/component/content/article',
		null,
		array(
			/**
			 * Renders attachment article content.
			 *
			 * @since 1.1.0
			 *
			 * @return void
			 */
			Template_Part::RENDER_CALLBACK => function () {
				get_template_part(
					'template-parts/header/page-header',
					null,
					array(
						Template_Part::HEADING => get_the_title(),
					)
				);
				get_template_part(
					'template-parts/component/content/entry-content',
					null,
					array(
						/**
						 * Renders entry content.
						 *
						 * @since 1.1.0
						 *
						 * @return void
						 */
						Template_Part::RENDER_CALLBACK => function () {
							Html_Builder::create()->create_figure(
								array(
									Html_Option::ATTRS => array(
										Html_Attr::CLASS_NAME => 'wp-block-image',
									),
								)
							)->render_element(
								function ( $html_builder ) {
									if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
										return;
									}

									echo wp_get_attachment_image(
										get_the_ID(),
										/**
										* Filters default image attachment size.
										*
										* @param string $image_size Image size. Default 'full'.
										*/
										apply_filters(
											Content_Hook::ATTACHMENT_SIZE,
											Content::ATTACHMENT_SIZE_FULL
										)
									);

									$attachment_caption = wp_get_attachment_caption();

									if ( Validator::is_non_empty_string( $attachment_caption ) ) {
										$html_builder->create_figcaption(
											array(
												Html_Option::ATTRS => array(
													Html_Attr::CLASS_NAME => 'wp-caption-text',
												),
											)
										)->render_element(
											esc_html( $attachment_caption )
										);
									}
								}
							);
							get_template_part( 'template-parts/component/content/link-pages' );
						},
					)
				);
				get_template_part( 'template-parts/footer/post-footer-attachment' );
			},
		)
	);
	get_template_part( 'template-parts/component/content/comments' );
}
