<?php

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

if ( class_exists( 'AIEO_SEO_Breadcrumb', false ) ) {
	return;
}

class AIEO_SEO_Breadcrumb {

	
	public static function register() {
		static $done = false;
		if ( $done ) {
			return;
		}
		$done = true;
		add_shortcode( 'aieo_breadcrumb', array( __CLASS__, 'shortcode' ) );
	}

	
	public static function items( $object_type, $object_id ) {
		$object_type = (string) $object_type;
		$object_id   = (int) $object_id;
		$home        = trailingslashit( home_url() );
		$items       = array( array( 'name' => get_bloginfo( 'name' ), 'url' => $home ) );

		if ( post_type_exists( $object_type ) ) {
			$post = get_post( $object_id );
			if ( ! $post instanceof WP_Post ) {
				return $items;
			}
			if ( 'product' === $object_type && taxonomy_exists( 'product_cat' ) ) {
				$leaf = self::primary_leaf( $object_id, 'product_cat' );
				if ( $leaf instanceof WP_Term ) {
					
					
					
					foreach ( self::term_trail( $leaf ) as $crumb ) {
						if ( '' !== (string) $crumb['url'] ) {
							$items[] = $crumb;
						}
					}
				}
			}
			$items[] = array( 'name' => get_the_title( $post ), 'url' => (string) get_permalink( $post ), 'current' => true );
			return $items;
		}

		if ( taxonomy_exists( $object_type ) ) {
			$term = get_term( $object_id, $object_type );
			if ( ! $term instanceof WP_Term ) {
				return $items;
			}
			foreach ( self::term_trail( $term ) as $i => $crumb ) {
				$items[] = $crumb;
			}
			
			$items[ count( $items ) - 1 ]['current'] = true;
			return $items;
		}

		return $items;
	}

	
	public static function current_object() {
		if ( is_singular() ) {
			$id = (int) get_queried_object_id();
			return $id > 0 ? array( (string) get_post_type( $id ), $id ) : null;
		}
		if ( is_category() || is_tag() || is_tax() ) {
			$t = get_queried_object();
			if ( $t instanceof WP_Term ) {
				return array( (string) $t->taxonomy, (int) $t->term_id );
			}
		}
		return null;
	}

	

	public static function shortcode( $atts ) {
		$atts = shortcode_atts( array( 'separator' => '›' ), is_array( $atts ) ? $atts : array(), 'aieo_breadcrumb' );
		return self::render_current( array( 'separator' => (string) $atts['separator'] ) );
	}

	
	public static function render_current( $args = array() ) {
		$resolved = self::current_object();
		if ( ! $resolved ) {
			return '';
		}
		$items = self::items( $resolved[0], $resolved[1] );
		if ( count( $items ) < 2 ) {
			return '';
		}
		$sep       = isset( $args['separator'] ) && '' !== $args['separator'] ? (string) $args['separator'] : '›';
		$class     = isset( $args['class'] ) ? (string) $args['class'] : 'aieo-breadcrumb';
		$last      = count( $items ) - 1;
		$out       = '<nav class="' . esc_attr( $class ) . '" aria-label="' . esc_attr__( 'Breadcrumb', 'ai-eshop-optimizer' ) . '"><ol class="aieo-breadcrumb__list">';
		foreach ( $items as $i => $crumb ) {
			$name = isset( $crumb['name'] ) ? (string) $crumb['name'] : '';
			$url  = isset( $crumb['url'] ) ? (string) $crumb['url'] : '';
			$is_current = ( $i === $last ) || ! empty( $crumb['current'] );
			$out .= '<li class="aieo-breadcrumb__item">';
			if ( $is_current ) {
				$out .= '<span class="aieo-breadcrumb__current" aria-current="page">' . esc_html( $name ) . '</span>';
			} elseif ( '' === $url ) {
				
				
				$out .= '<span class="aieo-breadcrumb__link">' . esc_html( $name ) . '</span>';
			} else {
				$out .= '<a class="aieo-breadcrumb__link" href="' . esc_url( $url ) . '">' . esc_html( $name ) . '</a>';
			}
			if ( $i !== $last ) {
				$out .= '<span class="aieo-breadcrumb__sep" aria-hidden="true"> ' . esc_html( $sep ) . ' </span>';
			}
			$out .= '</li>';
		}
		$out .= '</ol></nav>';
		return $out;
	}

	

	
	private static function primary_leaf( $object_id, $taxonomy ) {
		$cats = get_the_terms( $object_id, $taxonomy );
		if ( empty( $cats ) || is_wp_error( $cats ) ) {
			return null;
		}
		$leaf   = $cats[0];
		
		
		
		$pinned = ( class_exists( 'AIEO_SEO_Primary_Term' ) && method_exists( 'AIEO_SEO_Primary_Term', 'get_primary_cat_id' ) )
			? (int) AIEO_SEO_Primary_Term::get_primary_cat_id( 'product', (int) $object_id )
			: 0;
		if ( $pinned > 0 ) {
			foreach ( $cats as $c ) {
				if ( (int) $c->term_id === $pinned ) {
					$leaf = $c;
					break;
				}
			}
		}
		return $leaf;
	}

	
	private static function term_trail( $leaf ) {
		$out       = array();
		$ancestors = array_reverse( (array) get_ancestors( $leaf->term_id, $leaf->taxonomy, 'taxonomy' ) );
		foreach ( $ancestors as $aid ) {
			$at = get_term( (int) $aid, $leaf->taxonomy );
			if ( $at instanceof WP_Term ) {
				$l = get_term_link( $at );
				
				
				
				if ( ! is_wp_error( $l ) ) {
					$out[] = array( 'name' => $at->name, 'url' => (string) $l );
				}
			}
		}
		$ll    = get_term_link( $leaf );
		$out[] = array( 'name' => $leaf->name, 'url' => is_wp_error( $ll ) ? '' : (string) $ll );
		return $out;
	}
}
