<?php
/**
 * Custom navigation menu walker.
 *
 * @package Eronex
 * @since   0.0.1
 */

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

/**
 * Custom Walker class for the WordPress navigation menu.
 *
 * Adds custom HTML structure with a span around the anchor for hamburger
 * menus, and an icon for items that have dropdown children.
 *
 * @package Eronex
 * @since   0.0.1
 */
class Eronex_Walker_Nav_Menu extends Walker_Nav_Menu {

	/**
	 * Starts the element output, wrapping the anchor and dropdown icon as needed.
	 *
	 * @since 0.0.1
	 *
	 * @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. Used for padding.
	 * @param stdClass $args   An object of wp_nav_menu() arguments.
	 * @param int      $id     Current item ID.
	 * @return void
	 */
	public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
		$classes   = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;

		// Check if the item has children (dropdown)
		$has_children = ! empty( $item->classes ) && in_array( 'menu-item-has-children', $item->classes, true );

		// Add custom class for items with children
		if ( $has_children ) {
			$classes[] = 'has-dropdown'; // Add custom class if needed
		}

		/**
		 * Filters the CSS classes applied to a menu item's list item element.
		 *
		 * @since 0.0.1
		 *
		 * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
		 * @param WP_Post  $item    The current menu item.
		 * @param stdClass $args    An object of wp_nav_menu() arguments.
		 */
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

		/**
		 * Filters the ID applied to a menu item's list item element.
		 *
		 * @since 0.0.1
		 *
		 * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
		 * @param WP_Post  $item    The current menu item.
		 * @param stdClass $args    An object of wp_nav_menu() arguments.
		 */
		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

		// Generate anchor attributes
		$attributes  = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
		$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
		$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
		$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';

		// Start the <li> element
		$item_output  = $args->before;
		$item_output .= '<li' . $id . $class_names . '>';

		if ( 'eronex-hamburger-nav' === $args->container_id ) {
			$item_output .= '<span class="menu-item-span">';
		}

		// Open the anchor tag
		$item_output .= $args->before;
		$item_output .= '<a' . $attributes . '>';

		/**
		 * Filters the menu item title used in the navigation link.
		 *
		 * @since 0.0.1
		 *
		 * @param string $title The menu item title.
		 * @param int    $id    The post ID.
		 */
		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

		// Add the <i> icon inside the <a> if there are child items
		if ( $has_children && 'eronex-hamburger-nav' !== $args->container_id ) {
			$item_output .= '<i class="eronex-dropdown-icon" aria-hidden="true"></i>';
		}

		// Close the anchor tag
		$item_output .= '</a>';

		// Add a focusable submenu control for hamburger menus
		if ( $has_children && 'eronex-hamburger-nav' === $args->container_id ) {
			$submenu_label = sprintf(
				/* translators: %s: Parent menu item title. */
				__( 'Open %s submenu', 'eronex' ),
				wp_strip_all_tags( $item->title )
			);
			$item_output .= '<button type="button" class="eronex-dropdown-icon" aria-expanded="false" aria-label="' . esc_attr( $submenu_label ) . '"></button>';
		}

		if ( 'eronex-hamburger-nav' === $args->container_id ) {
			$item_output .= '</span>';
		}

		// Close the <span> element
		$item_output .= $args->after;

		$item_output .= $args->after;

		/**
		 * Filters the HTML output of a walker nav menu start element.
		 *
		 * @since 0.0.1
		 *
		 * @param string   $item_output The menu item's starting HTML output.
		 * @param WP_Post  $item        Menu item data object.
		 * @param int      $depth       Depth of menu item. Used for padding.
		 * @param stdClass $args        An object of wp_nav_menu() arguments.
		 */
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}

	/**
	 * Starts the list before the elements are added (submenu level).
	 *
	 * Optionally prepends a dropdown icon when the parent has children,
	 * then delegates to the parent walker.
	 *
	 * @since 0.0.1
	 *
	 * @param string   $output Used to append additional content (passed by reference).
	 * @param int      $depth  Depth of menu item. Used for padding.
	 * @param stdClass $args   An object of wp_nav_menu() arguments.
	 * @return void
	 */
	public function start_lvl( &$output, $depth = 0, $args = null ) {
		if ( isset( $args->has_children ) && $args->has_children ) {
			// Optionally add <i> for dropdown
			$output .= '<i class="eronex-dropdown-icon"></i>';
		}
		parent::start_lvl( $output, $depth, $args );
	}
}
