<?php
/**
 * Template part: breadcrumbs
 *
 * Simple breadcrumb navigation with BreadcrumbList schema markup.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 */

declare(strict_types=1);

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

$home_url   = home_url( '/' );
$home_label = __( 'Home', 'trx-auto-dealer' );

$archive_url   = get_post_type_archive_link( 'trx_ad_listing' );
$archive_label = __( 'Inventory', 'trx-auto-dealer' );

$current_title = get_the_title();
$items         = array();

$items[] = array(
	'url'  => $home_url,
	'name' => $home_label,
);

if ( false !== $archive_url ) {
	$items[] = array(
		'url'  => $archive_url,
		'name' => $archive_label,
	);
}

$items[] = array(
	'url'  => '',
	'name' => $current_title,
);

/**
 * Filter the breadcrumb items.
 *
 * @param array<int, array{url: string, name: string}> $items Breadcrumb items.
 */
$items = apply_filters( 'trx_ad_breadcrumb_items', $items );
?>

<nav class="trx-ad-breadcrumbs" aria-label="<?php esc_attr_e( 'Breadcrumb', 'trx-auto-dealer' ); ?>">
	<ol class="trx-ad-breadcrumbs__list" itemscope itemtype="https://schema.org/BreadcrumbList">
		<?php foreach ( $items as $position => $item ) : ?>
			<li class="trx-ad-breadcrumbs__item" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
				<?php if ( '' !== $item['url'] && $position < count( $items ) - 1 ) : ?>
					<a href="<?php echo esc_url( $item['url'] ); ?>" class="trx-ad-breadcrumbs__link" itemprop="item">
						<span itemprop="name"><?php echo esc_html( $item['name'] ); ?></span>
					</a>
				<?php else : ?>
					<span class="trx-ad-breadcrumbs__current" itemprop="item">
						<span itemprop="name"><?php echo esc_html( $item['name'] ); ?></span>
					</span>
				<?php endif; ?>
				<meta itemprop="position" content="<?php echo esc_attr( (string) ( $position + 1 ) ); ?>">
			</li>
		<?php endforeach; ?>
	</ol>
</nav>
