<?php
/**
 * Sifency Advanced Breadcrumbs Widget
 *
 * @package     Sifency_Addons
 * @since       1.0.1
 * @version     1.0.5
 */

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

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;

/**
 * Class Sifency_Addons_Universal_Breadcrumbs
 *
 * Renders a fully customisable breadcrumb trail with 10 pre-built
 * visual styles, rich Elementor style controls, and native WordPress
 * breadcrumb logic — no third-party plugin dependency required.
 */
class Sifency_Addons_Universal_Breadcrumbs extends Sifency_Addons_Widget_Base {

	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Animation_Controls;

	// -------------------------------------------------------------------------
	// Boot
	// -------------------------------------------------------------------------

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );

		$this->wg_config = array(
			'name'          => 'sifency-breadcrumbs',
			'title'         => 'Sifency Advanced Breadcrumbs',
			'icon'          => 'eicon-product-breadcrumbs',
			'category'      => array( 'sifency-addons-widgets' ),
			'style_depends' => array( 'sifency-breadcrumbs-style' ),
		);
	}

	// =========================================================================
	// Controls
	// =========================================================================

	protected function register_controls() {

		// ---------------------------------------------------------------------
		// SECTION: Layout & Style Preset
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_layout',
			array(
				'label' => esc_html__( 'Layout & Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'breadcrumb_style',
			array(
				'label'   => esc_html__( 'Pre-built Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-classic',
				'options' => array(
					'style-classic'    => esc_html__( '1. Classic Clean', 'sifency-addons' ),
					'style-pill-bar'   => esc_html__( '2. Modern Pill Bar', 'sifency-addons' ),
					'style-individual' => esc_html__( '3. Individual Pills', 'sifency-addons' ),
					'style-chevron'    => esc_html__( '4. Chevron Steps', 'sifency-addons' ),
					'style-glass'      => esc_html__( '5. Glassmorphism', 'sifency-addons' ),
					'style-neumorph'   => esc_html__( '6. Neumorphism', 'sifency-addons' ),
					'style-underline'  => esc_html__( '7. Bottom Line Hover', 'sifency-addons' ),
					'style-brutalism'  => esc_html__( '8. Brutalism', 'sifency-addons' ),
					'style-slash'      => esc_html__( '9. Minimal Slash', 'sifency-addons' ),
					'style-cyberpunk'  => esc_html__( '10. Glowing Cyberpunk', 'sifency-addons' ),
				),
			)
		);

		$this->add_responsive_control(
			'align',
			array(
				'label'                => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'                 => Controls_Manager::CHOOSE,
				'options'              => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'              => 'flex-start',
				'selectors'            => array(
					'{{WRAPPER}} .sifency-breadcrumbs-wrap' => 'justify-content: {{VALUE}};',
				),
				'selectors_dictionary' => array(
					'left'   => 'flex-start',
					'center' => 'center',
					'right'  => 'flex-end',
				),
			)
		);

		$this->add_control(
			'width_control',
			array(
				'label'        => esc_html__( 'List Width', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'inline',
				'options'      => array(
					'inline' => esc_html__( 'Auto (Inline)', 'sifency-addons' ),
					'full'   => esc_html__( 'Full Width', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-bc-width-',
			)
		);

		$this->end_controls_section();

		// ---------------------------------------------------------------------
		// SECTION: Home Item
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_home',
			array(
				'label' => esc_html__( 'Home Item', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'show_home',
			array(
				'label'     => esc_html__( 'Show Home Link', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_on'  => esc_html__( 'Show', 'sifency-addons' ),
				'label_off' => esc_html__( 'Hide', 'sifency-addons' ),
				'default'   => 'yes',
			)
		);

		$this->add_control(
			'home_text',
			array(
				'label'       => esc_html__( 'Home Label', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Home', 'sifency-addons' ),
				'placeholder' => esc_html__( 'Home', 'sifency-addons' ),
				'dynamic'     => array( 'active' => true ),
				'condition'   => array( 'show_home' => 'yes' ),
			)
		);

		$this->add_control(
			'home_icon',
			array(
				'label'     => esc_html__( 'Home Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-home',
					'library' => 'fa-solid',
				),
				'condition' => array( 'show_home' => 'yes' ),
			)
		);

		$this->add_control(
			'home_icon_position',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'before',
				'options'   => array(
					'before' => esc_html__( 'Before Text', 'sifency-addons' ),
					'after'  => esc_html__( 'After Text', 'sifency-addons' ),
					'only'   => esc_html__( 'Icon Only', 'sifency-addons' ),
				),
				'condition' => array( 'show_home' => 'yes' ),
			)
		);

		$this->end_controls_section();

		// ---------------------------------------------------------------------
		// SECTION: Separator
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_separator',
			array(
				'label' => esc_html__( 'Separator', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'separator_type',
			array(
				'label'   => esc_html__( 'Separator Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'icon',
				'options' => array(
					'icon' => esc_html__( 'Icon', 'sifency-addons' ),
					'text' => esc_html__( 'Text / Symbol', 'sifency-addons' ),
					'none' => esc_html__( 'None', 'sifency-addons' ),
				),
			)
		);

		$this->add_control(
			'separator_icon',
			array(
				'label'     => esc_html__( 'Separator Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-angle-right',
					'library' => 'fa-solid',
				),
				'condition' => array( 'separator_type' => 'icon' ),
			)
		);

		$this->add_control(
			'separator_text',
			array(
				'label'     => esc_html__( 'Separator Symbol', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '/',
				'condition' => array( 'separator_type' => 'text' ),
			)
		);

		$this->end_controls_section();

		// ---------------------------------------------------------------------
		// SECTION: Trail Items (visibility)
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_trail',
			array(
				'label' => esc_html__( 'Trail Visibility', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'show_parent',
			array(
				'label'     => esc_html__( 'Show Parent Pages / Categories', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_on'  => esc_html__( 'Show', 'sifency-addons' ),
				'label_off' => esc_html__( 'Hide', 'sifency-addons' ),
				'default'   => 'yes',
			)
		);

		$this->add_control(
			'show_current',
			array(
				'label'     => esc_html__( 'Show Current Page', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_on'  => esc_html__( 'Show', 'sifency-addons' ),
				'label_off' => esc_html__( 'Hide', 'sifency-addons' ),
				'default'   => 'yes',
			)
		);

		$this->add_control(
			'max_text_length',
			array(
				'label'       => esc_html__( 'Max Label Length (chars)', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'default'     => 0,
				'min'         => 0,
				'max'         => 200,
				'step'        => 1,
				'description' => esc_html__( '0 = unlimited. Longer labels will be truncated with an ellipsis.', 'sifency-addons' ),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_help_controls();

		// =====================================================================
		// STYLE TAB
		// =====================================================================

		// ---------------------------------------------------------------------
		// STYLE: Wrapper
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_style_wrapper',
			array(
				'label' => esc_html__( 'Wrapper', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'wrapper_background',
				'label'    => esc_html__( 'Wrapper Background', 'sifency-addons' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .sifency-breadcrumbs-list',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'wrapper_border',
				'selector' => '{{WRAPPER}} .sifency-breadcrumbs-list',
			)
		);

		$this->add_responsive_control(
			'wrapper_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'wrapper_box_shadow',
				'selector' => '{{WRAPPER}} .sifency-breadcrumbs-list',
			)
		);

		$this->add_responsive_control(
			'wrapper_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'wrapper_margin',
			array(
				'label'      => esc_html__( 'Margin', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// ---------------------------------------------------------------------
		// STYLE: Breadcrumb Items
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_style_items',
			array(
				'label' => esc_html__( 'Breadcrumb Items', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-breadcrumbs-list li a, {{WRAPPER}} .sifency-breadcrumbs-list li span, {{WRAPPER}} .sifency-breadcrumbs-list li.current-item',
			)
		);

		$this->add_responsive_control(
			'item_gap',
			array(
				'label'      => esc_html__( 'Item Gap', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
					'em' => array(
						'min' => 0,
						'max' => 5,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 6,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->start_controls_tabs( 'tabs_item_colors' );

		$this->start_controls_tab(
			'tab_color_normal',
			array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) )
		);

		$this->add_control(
			'color_text',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list a'    => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-breadcrumbs-list a svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'color_bg',
			array(
				'label'     => esc_html__( 'Item Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'item_border',
				'selector' => '{{WRAPPER}} .sifency-breadcrumbs-list li',
			)
		);

		$this->add_responsive_control(
			'item_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => esc_html__( 'Item Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tab_color_hover',
			array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) )
		);

		$this->add_control(
			'color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list a:hover'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-breadcrumbs-list a:hover svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'color_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'hover_transition',
			array(
				'label'      => esc_html__( 'Transition Duration (s)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 's' ),
				'range'      => array(
					's' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.05,
					),
				),
				'default'    => array(
					'unit' => 's',
					'size' => 0.3,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list a, {{WRAPPER}} .sifency-breadcrumbs-list li' => 'transition: all {{SIZE}}{{UNIT}} ease;',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tab_color_current',
			array( 'label' => esc_html__( 'Current', 'sifency-addons' ) )
		);

		$this->add_control(
			'color_current',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li.sifency-bc-current' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'color_bg_current',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li.sifency-bc-current' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'current_font_weight',
			array(
				'label'     => esc_html__( 'Font Weight', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '600',
				'options'   => array(
					'400'     => esc_html__( 'Normal (400)', 'sifency-addons' ),
					'500'     => esc_html__( 'Medium (500)', 'sifency-addons' ),
					'600'     => esc_html__( 'Semi-Bold (600)', 'sifency-addons' ),
					'700'     => esc_html__( 'Bold (700)', 'sifency-addons' ),
					'inherit' => esc_html__( 'Inherit', 'sifency-addons' ),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li.sifency-bc-current' => 'font-weight: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();

		// ---------------------------------------------------------------------
		// STYLE: Separator
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_style_separator',
			array(
				'label' => esc_html__( 'Separator', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'color_sep',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-breadcrumbs-separator'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-breadcrumbs-separator svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'separator_size',
			array(
				'label'      => esc_html__( 'Icon / Font Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 6,
						'max' => 40,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-separator'     => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-breadcrumbs-separator svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// ---------------------------------------------------------------------
		// STYLE: Icon
		// ---------------------------------------------------------------------
		$this->start_controls_section(
			'section_style_icon',
			array(
				'label' => esc_html__( 'Item Icon', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'      => esc_html__( 'Icon Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 8,
						'max' => 60,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-breadcrumbs-list li a i'    => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-breadcrumbs-list li a svg'  => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'icon_gap',
			array(
				'label'      => esc_html__( 'Icon → Text Gap', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 20,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 5,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-bc-icon-before .sifency-bc-icon' => 'margin-right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-bc-icon-after  .sifency-bc-icon' => 'margin-left: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		$this->add_Sifency_Addons_Animation_Controls();
	}

	// =========================================================================
	// Trail Builder
	// =========================================================================

	/**
	 * Build the breadcrumb trail array using native WordPress conditional tags.
	 * No plugin dependency required.
	 *
	 * @param array $settings Widget settings.
	 * @return array  Each entry: ['url' => string, 'text' => string, 'icon' => array|null].
	 */
	private function generate_breadcrumbs_trail( array $settings ): array {
		$trail      = array();
		$max_length = absint( $settings['max_text_length'] ?? 0 );

		// Helper closure: truncate label when max length is set.
		$truncate = function ( string $text ) use ( $max_length ): string {
			if ( $max_length > 0 && mb_strlen( $text ) > $max_length ) {
				return mb_substr( $text, 0, $max_length ) . '&hellip;';
			}
			return $text;
		};

		if ( 'yes' === $settings['show_home'] ) {
			$trail[] = array(
				'url'  => home_url( '/' ),
				'text' => $truncate( sanitize_text_field( $settings['home_text'] ) ),
				'icon' => $settings['home_icon'] ?? null,
			);
		}

		if ( 'yes' === $settings['show_parent'] ) {
			if ( is_page() ) {
				$ancestors = array_reverse( get_post_ancestors( get_the_ID() ) );
				foreach ( $ancestors as $ancestor_id ) {
					$trail[] = array(
						'url'  => get_permalink( $ancestor_id ),
						'text' => $truncate( get_the_title( $ancestor_id ) ),
						'icon' => null,
					);
				}
			} elseif ( is_single() ) {
				$post_type = get_post_type();

				if ( 'post' === $post_type ) {
					// Standard posts: show primary category.
					$categories = get_the_category();
					if ( ! empty( $categories ) ) {
						$trail[] = array(
							'url'  => get_category_link( $categories[0]->term_id ),
							'text' => $truncate( $categories[0]->name ),
							'icon' => null,
						);
					}
				} else {
					// Custom post types: show CPT archive link.
					$post_type_obj = get_post_type_object( $post_type );
					if ( $post_type_obj && $post_type_obj->has_archive ) {
						$trail[] = array(
							'url'  => get_post_type_archive_link( $post_type ),
							'text' => $truncate( $post_type_obj->labels->name ),
							'icon' => null,
						);
					}

					// Show taxonomy terms for CPTs if applicable.
					$taxonomies = get_object_taxonomies( $post_type, 'objects' );
					foreach ( $taxonomies as $taxonomy ) {
						if ( ! $taxonomy->public ) {
							continue;
						}
						$terms = get_the_terms( get_the_ID(), $taxonomy->name );
						if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
							$trail[] = array(
								'url'  => get_term_link( $terms[0] ),
								'text' => $truncate( $terms[0]->name ),
								'icon' => null,
							);
							break;
						}
					}
				}
			} elseif ( is_tax() || is_category() || is_tag() ) {
				// Term hierarchy.
				$term    = get_queried_object();
				$parents = array();
				$parent  = $term->parent ?? 0;
				while ( $parent ) {
					$parent_term = get_term( $parent, $term->taxonomy );
					if ( $parent_term && ! is_wp_error( $parent_term ) ) {
						array_unshift( $parents, $parent_term );
						$parent = $parent_term->parent;
					} else {
						break;
					}
				}
				foreach ( $parents as $parent_term ) {
					$trail[] = array(
						'url'  => get_term_link( $parent_term ),
						'text' => $truncate( $parent_term->name ),
						'icon' => null,
					);
				}
			}
		}

		if ( 'yes' === $settings['show_current'] ) {
			$current_text = '';

			if ( is_page() || is_single() ) {
				$current_text = get_the_title();
			} elseif ( is_category() ) {
				$current_text = single_cat_title( '', false );
			} elseif ( is_tag() ) {
				$current_text = single_tag_title( '', false );
			} elseif ( is_tax() ) {
				$current_text = single_term_title( '', false );
			} elseif ( is_search() ) {
				/* translators: %s: search query string */
				$current_text = sprintf( esc_html__( 'Search: %s', 'sifency-addons' ), get_search_query() );
			} elseif ( is_404() ) {
				$current_text = esc_html__( '404 — Not Found', 'sifency-addons' );
			} elseif ( is_post_type_archive() ) {
				$current_text = post_type_archive_title( '', false );
			} elseif ( is_day() ) {
				$current_text = get_the_date();
			} elseif ( is_month() ) {
				$current_text = get_the_date( _x( 'F Y', 'monthly archive date format', 'sifency-addons' ) );
			} elseif ( is_year() ) {
				$current_text = get_the_date( _x( 'Y', 'yearly archive date format', 'sifency-addons' ) );
			} elseif ( is_author() ) {
				$current_text = get_the_author();
			}

			if ( '' !== $current_text ) {
				$trail[] = array(
					'url'  => '',
					'text' => $truncate( $current_text ),
					'icon' => null,
				);
			}
		}

		/**
		 * Filter the breadcrumb trail array before render.
		 *
		 * @param array $trail    Array of crumb items.
		 * @param array $settings Widget settings.
		 */
		return apply_filters( 'sifency_breadcrumbs_trail', $trail, $settings );
	}

	// =========================================================================
	// Render
	// =========================================================================

	protected function render_dom( array $context ) {
		$settings = $this->get_settings_for_display();
		$trail    = $this->generate_breadcrumbs_trail( $settings );
		$style    = esc_attr( $settings['breadcrumb_style'] );
		$sep_type = $settings['separator_type'];
		$icon_pos = $settings['home_icon_position'] ?? 'before';

		// Bail early if nothing to render.
		if ( empty( $trail ) ) {
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_target  = $settings['sifency_animation_target'] ?? 'wrapper';

		// Build nav element attributes.
		$nav_classes = array( 'sifency-breadcrumbs-wrap', $style );

		if ( 'wrapper' === $animation_target ) {
			$nav_attrs = $animation_manager->get_wrapper_attributes( $nav_classes );
		} else {
			$nav_attrs = sprintf( ' class="%s"', esc_attr( implode( ' ', array_filter( $nav_classes ) ) ) );
		}

		?>
		<nav<?php echo wp_kses_post( $nav_attrs ); ?> aria-label="<?php esc_attr_e( 'Breadcrumb', 'sifency-addons' ); ?>">
			<ol class="sifency-breadcrumbs-list" itemscope itemtype="https://schema.org/BreadcrumbList">
				<?php
				$total = count( $trail );

				foreach ( $trail as $index => $crumb ) :
					$is_last  = ( $index === $total - 1 );
					$is_home  = ( $index === 0 && 'yes' === $settings['show_home'] );
					$has_url  = ! empty( $crumb['url'] );
					$has_icon = $is_home && ! empty( $crumb['icon']['value'] );

					// Build <li> class list.
					$li_classes = array( 'sifency-breadcrumbs-item' );

					if ( $is_last ) {
						$li_classes[] = 'sifency-bc-current';
					}
					if ( $is_home ) {
						$li_classes[] = 'sifency-bc-home';
					}
					if ( $has_icon ) {
						$li_classes[] = 'sifency-bc-icon-' . esc_attr( $icon_pos );
					}

					// Animation attributes for <li>.
					if ( 'child' === $animation_target ) {
						$li_attrs = $animation_manager->get_item_attributes( $li_classes );
					} else {
						$li_attrs = sprintf( ' class="%s"', esc_attr( implode( ' ', array_filter( $li_classes ) ) ) );
					}

					// Schema.org position (1-based, separator items not counted).
					$position = $index + 1;
					?>

					<li<?php echo wp_kses_post( $li_attrs ); ?>
						itemprop="itemListElement"
						itemscope
						itemtype="https://schema.org/ListItem">

						<?php if ( $has_url ) : ?>
							<a href="<?php echo esc_url( $crumb['url'] ); ?>"
								itemprop="item">
								<?php $this->render_crumb_inner( $crumb, $has_icon, $icon_pos ); ?>
							</a>
							<meta itemprop="name" content="<?php echo esc_attr( wp_strip_all_tags( $crumb['text'] ) ); ?>">
						<?php else : ?>
							<span itemprop="item">
								<?php $this->render_crumb_inner( $crumb, $has_icon, $icon_pos ); ?>
							</span>
							<meta itemprop="name" content="<?php echo esc_attr( wp_strip_all_tags( $crumb['text'] ) ); ?>">
						<?php endif; ?>

						<meta itemprop="position" content="<?php echo esc_attr( $position ); ?>">

					</li>

					<?php if ( ! $is_last && 'none' !== $sep_type ) : ?>
						<li class="sifency-breadcrumbs-separator" aria-hidden="true">
							<?php $this->render_separator( $settings ); ?>
						</li>
					<?php endif; ?>

				<?php endforeach; ?>
			</ol>
		</nav>
		<?php
	}

	// =========================================================================
	// Private Render Helpers
	// =========================================================================

	/**
	 * Render icon + text inside a crumb item, honouring icon position.
	 *
	 * @param array  $crumb    The crumb data array.
	 * @param bool   $has_icon Whether the crumb has an icon.
	 * @param string $icon_pos 'before' | 'after' | 'only'.
	 */
	private function render_crumb_inner( array $crumb, bool $has_icon, string $icon_pos ): void {
		$text_html = '<span class="crumb-text">' . wp_kses_post( $crumb['text'] ) . '</span>';

		if ( ! $has_icon ) {
			echo $text_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return;
		}

		$icon_html = '<span class="sifency-bc-icon">';
		ob_start();
		\Elementor\Icons_Manager::render_icon( $crumb['icon'], array( 'aria-hidden' => 'true' ) );
		$icon_html .= ob_get_clean() . '</span>';

		if ( 'only' === $icon_pos ) {
			echo $icon_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		} elseif ( 'after' === $icon_pos ) {
			echo $text_html . $icon_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		} else {
			// Default: before.
			echo $icon_html . $text_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	}

	/**
	 * Render the separator element (icon or text).
	 *
	 * @param array $settings Widget settings.
	 */
	private function render_separator( array $settings ): void {
		if ( 'icon' === $settings['separator_type'] && ! empty( $settings['separator_icon']['value'] ) ) {
			\Elementor\Icons_Manager::render_icon(
				$settings['separator_icon'],
				array( 'aria-hidden' => 'true' )
			);
		} elseif ( 'text' === $settings['separator_type'] ) {
			echo esc_html( $settings['separator_text'] );
		}
	}
}
