<?php
/**
 * Template part for displaying post categories
 *
 * @package errigal
 * @since 1.0.0
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Component_Class as Component_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Shared\Errigal_Html_Builder as Html_Builder;

if ( ! Content::has_category_support() ) {
	return;
}

Html_Builder::create()->create_ul(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => 'post-categories',
			Html_Attr::ARIA_LABEL => __( 'Categories', 'errigal' ),
		),
	)
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		if ( has_category() ) {
			foreach ( get_the_category() as $post_category ) {
				if ( ! Content::is_wp_term( $post_category ) ) {
					continue;
				}

				$html_builder->create_li()->render_element(
					function ( $html_builder ) use ( $post_category ) {
						if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
							return;
						}

						$html_builder->create_a(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => Component_Class::CHIP,
									Html_Attr::HREF       => get_category_link( $post_category ),
								),
							)
						)->render_element(
							esc_html( $post_category->name )
						);
					}
				);
			}

			return;
		}

		$html_builder->create_li(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'uncategorised',
				),
			)
		)->render_element(
			esc_html__( 'Uncategorised', 'errigal' )
		);
	}
);
