<?php
/**
 * Bootstrap 5 Nav Walker + Mega Menu Render.
 *
 * Extends the default WordPress `Walker_Nav_Menu` class to provide
 * Bootstrap 5 compatible markup and support for custom mega menus.
 *
 * @package    Wira
 * @since      1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Custom Bootstrap Nav Walker.
 *
 * Generates Bootstrap 5 nav markup with support for dropdowns and
 * mega menus using custom menu item meta fields.
 *
 * @since 1.0.0
 */
class Bootstrap_Navwalker extends Walker_Nav_Menu {

	/**
	 * Start Level.
	 *
	 * Adds the wrapper <ul> for submenus.
	 *
	 * @since 1.0.0
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param int    $depth  Depth of menu item.
	 * @param array  $args   Additional arguments.
	 */
	public function start_lvl( &$output, $depth = 0, $args = array() ) {
		$indent  = str_repeat( "\t", $depth );
		$submenu = ( $depth > 0 ) ? 'dropdown-submenu' : 'dropdown-menu';
		$output .= "\n$indent<ul class=\"" . esc_attr( $submenu ) . "\">\n";
	}

	/**
	 * Start Element.
	 *
	 * Adds markup for a single menu item, with support for mega menu injection.
	 *
	 * @since 1.0.0
	 *
	 * @param string   $output Used to append additional content (passed by reference).
	 * @param \WP_Post $item   Menu item data object.
	 * @param int      $depth  Depth of menu item.
	 * @param array    $args   Additional arguments.
	 * @param int      $id     Current item ID.
	 */
	public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

		// === Mega menu flags from meta ===.
		$enable_mega = get_post_meta( $item->ID, '_wira_enable_mega_menu', true );
		$mega_id     = get_post_meta( $item->ID, '_wira_mega_menu', true );
		$has_mega    = ( 0 === $depth && $enable_mega && $mega_id );

		// === Classes for <li> ===.
		$classes   = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'nav-item';

		if ( $has_mega ) {
			$classes[] = 'has-mega-menu dropdown theme-dropdown';
		} else {
			if ( in_array( 'menu-item-has-children', $classes, true ) && 0 === $depth ) {
				$classes[] = 'dropdown theme-dropdown';
			}
			if ( $depth > 0 ) {
				$classes[] = 'dropdown-item theme-dropdown';
			}
		}

		$class_names = ' class="' . esc_attr( implode( ' ', array_filter( $classes ) ) ) . '"';

		$output .= '<li' . $class_names . '>';

		// === Link attributes ===.
		$atts           = array();
		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
		$atts['target'] = ! empty( $item->target ) ? $item->target : '';
		$atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';
		$atts['href']   = ! empty( $item->url ) ? $item->url : '';

		if ( $has_mega ) {
			// Top-level item with mega menu → behave like dropdown toggle.
			$atts['class']         = 'nav-link theme-accent-text dropdown-toggle';
			$atts['aria-current']  = 'page';
			$atts['aria-expanded'] = 'false';
		} elseif ( in_array( 'menu-item-has-children', $classes, true ) ) {
			if ( 0 === $depth ) {
				$atts['class']         = 'nav-link theme-accent-text dropdown-toggle';
				$atts['aria-current']  = 'page';
				$atts['aria-expanded'] = 'false';
			} else {
				$atts['class']        = 'nav-link theme-accent-text dropdown-toggle-submenu';
				$atts['aria-current'] = 'page';
			}
		} else {
			$atts['class']        = 'nav-link theme-accent-text';
			$atts['aria-current'] = 'page';
		}

		// Allow filters to modify link attributes.
		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

		$attributes = '';
		foreach ( $atts as $attr => $value ) {
			if ( '' !== $value && null !== $value ) {
				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
				$attributes .= ' ' . $attr . '="' . $value . '"';
			}
		}

		$title = apply_filters( 'the_title', $item->title, $item->ID );

		$item_output  = $args->before;
		$item_output .= '<a' . $attributes . '>';
		$item_output .= $args->link_before . esc_html( $title ) . $args->link_after;
		$item_output .= '</a>';
		$item_output .= $args->after;

		// === Inject mega menu content ===.
		if ( $has_mega
			&& class_exists( 'Wtoolkit_Template_Builder_Helper' )
			&& method_exists( 'Wtoolkit_Template_Builder_Helper', 'wirabase_render_elementor_template' )
		) {
			$item_output .= '<div class="theme-mega-menu" data-id="' . esc_attr( $mega_id ) . '">';
			$item_output .= \Wtoolkit_Template_Builder_Helper::wirabase_render_elementor_template( (int) $mega_id );
			$item_output .= '</div>';
		}

		$output .= $item_output;
	}
}
