<?php
/**
 * Breadcrumb helpers.
 *
 * @package ttsunsolar
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Blog / posts index label + URL.
 *
 * @return array{url:string,label:string}
 */
function ttsunsolar_get_blog_crumb() {
	$posts_page = (int) get_option( 'page_for_posts' );
	if ( $posts_page ) {
		return array(
			'url'   => get_permalink( $posts_page ),
			'label' => get_the_title( $posts_page ),
		);
	}
	return array(
		'url'   => home_url( '/' ),
		'label' => __( 'Blog', 'ttsunsolar' ),
	);
}

/**
 * Append hierarchical term ancestors to breadcrumb items.
 *
 * @param array   $items    Items by reference.
 * @param WP_Term $term     Current term.
 */
function ttsunsolar_append_term_ancestors( &$items, $term ) {
	if ( ! ( $term instanceof WP_Term ) || ! is_taxonomy_hierarchical( $term->taxonomy ) ) {
		return;
	}

	$ancestors = array_reverse( get_ancestors( $term->term_id, $term->taxonomy ) );
	foreach ( $ancestors as $ancestor_id ) {
		$ancestor = get_term( (int) $ancestor_id, $term->taxonomy );
		if ( $ancestor instanceof WP_Term && ! is_wp_error( $ancestor ) ) {
			$items[] = array(
				'url'   => get_term_link( $ancestor ),
				'label' => $ancestor->name,
			);
		}
	}
}

/**
 * Build breadcrumb items for the current request.
 *
 * @return array<int, array{url?:string,label:string,current?:bool}>
 */
function ttsunsolar_get_breadcrumb_items() {
	if ( is_front_page() ) {
		return array();
	}

	$items = array(
		array(
			'url'   => home_url( '/' ),
			'label' => __( 'Home', 'ttsunsolar' ),
		),
	);

	if ( is_home() && ! is_front_page() ) {
		$blog    = ttsunsolar_get_blog_crumb();
		$items[] = array(
			'label'   => $blog['label'],
			'current' => true,
		);
		return $items;
	}

	if ( is_singular() ) {
		$post_id = get_the_ID();

		if ( is_single() && 'post' === get_post_type( $post_id ) ) {
			$blog    = ttsunsolar_get_blog_crumb();
			$items[] = array(
				'url'   => $blog['url'],
				'label' => $blog['label'],
			);

			$cats = get_the_category( $post_id );
			if ( ! empty( $cats ) && ! is_wp_error( $cats ) ) {
				$cat = $cats[0];
				ttsunsolar_append_term_ancestors( $items, $cat );
				$items[] = array(
					'url'   => get_term_link( $cat ),
					'label' => $cat->name,
				);
			}
		} elseif ( is_page() && $post_id ) {
			$ancestors = array_reverse( get_post_ancestors( $post_id ) );
			foreach ( $ancestors as $ancestor_id ) {
				$items[] = array(
					'url'   => get_permalink( $ancestor_id ),
					'label' => get_the_title( $ancestor_id ),
				);
			}
		} elseif ( is_singular() && ! is_page() ) {
			$post_type = get_post_type_object( get_post_type( $post_id ) );
			if ( $post_type && ! empty( $post_type->has_archive ) ) {
				$items[] = array(
					'url'   => get_post_type_archive_link( $post_type->name ),
					'label' => $post_type->labels->name,
				);
			}
		}

		$items[] = array(
			'label'   => get_the_title( $post_id ),
			'current' => true,
		);
		return $items;
	}

	if ( is_category() || is_tag() || is_tax() ) {
		$term = get_queried_object();
		if ( $term instanceof WP_Term ) {
			if ( in_array( $term->taxonomy, array( 'category', 'post_tag' ), true ) ) {
				$blog    = ttsunsolar_get_blog_crumb();
				$items[] = array(
					'url'   => $blog['url'],
					'label' => $blog['label'],
				);
			} else {
				$taxonomy = get_taxonomy( $term->taxonomy );
				if ( $taxonomy && ! empty( $taxonomy->object_type ) ) {
					$post_type = get_post_type_object( $taxonomy->object_type[0] );
					if ( $post_type && ! empty( $post_type->has_archive ) ) {
						$items[] = array(
							'url'   => get_post_type_archive_link( $post_type->name ),
							'label' => $post_type->labels->name,
						);
					}
				}
			}

			ttsunsolar_append_term_ancestors( $items, $term );
			$items[] = array(
				'label'   => $term->name,
				'current' => true,
			);
		}
		return $items;
	}

	if ( is_author() ) {
		$items[] = array(
			'label'   => get_the_archive_title(),
			'current' => true,
		);
		return $items;
	}

	if ( is_search() ) {
		$items[] = array(
			'label'   => sprintf(
				/* translators: %s: search query */
				__( 'Search results for “%s”', 'ttsunsolar' ),
				get_search_query()
			),
			'current' => true,
		);
		return $items;
	}

	if ( is_404() ) {
		$items[] = array(
			'label'   => __( 'Page not found', 'ttsunsolar' ),
			'current' => true,
		);
		return $items;
	}

	if ( is_archive() ) {
		$items[] = array(
			'label'   => wp_strip_all_tags( get_the_archive_title() ),
			'current' => true,
		);
	}

	return $items;
}

/**
 * Whether breadcrumbs should render.
 *
 * @return bool
 */
function ttsunsolar_should_show_breadcrumbs() {
	return ! is_front_page() && count( ttsunsolar_get_breadcrumb_items() ) > 1;
}

/**
 * Output breadcrumb navigation.
 */
function ttsunsolar_render_breadcrumbs() {
	if ( ! ttsunsolar_should_show_breadcrumbs() ) {
		return;
	}

	$items = ttsunsolar_get_breadcrumb_items();
	?>
	<nav class="ttsunsolar-breadcrumb" aria-label="<?php esc_attr_e( 'Breadcrumb', 'ttsunsolar' ); ?>">
		<ol class="ttsunsolar-breadcrumb__list">
			<?php foreach ( $items as $index => $item ) : ?>
				<li class="ttsunsolar-breadcrumb__item<?php echo ! empty( $item['current'] ) ? ' is-current' : ''; ?>">
					<?php if ( $index > 0 ) : ?>
						<span class="ttsunsolar-breadcrumb__sep" aria-hidden="true">/</span>
					<?php endif; ?>
					<?php if ( empty( $item['current'] ) && ! empty( $item['url'] ) ) : ?>
						<a href="<?php echo esc_url( $item['url'] ); ?>"><?php echo esc_html( $item['label'] ); ?></a>
					<?php else : ?>
						<span class="ttsunsolar-breadcrumb__current"<?php echo ! empty( $item['current'] ) ? ' aria-current="page"' : ''; ?>><?php echo esc_html( wp_strip_all_tags( $item['label'] ) ); ?></span>
					<?php endif; ?>
				</li>
			<?php endforeach; ?>
		</ol>
	</nav>
	<?php
}

/**
 * Page / archive heading title for content area.
 *
 * @return string
 */
function ttsunsolar_get_content_title() {
	return ttsunsolar_get_inner_banner_title();
}
