<?php
/**
 * Bootstrap Nav Walker – keyboard-accessible
 */
class Bootstrap_Navwalker extends Walker_Nav_Menu {

    public function start_lvl( &$output, $depth = 0, $args = null ) {
        $indent        = str_repeat( "\t", $depth );
        $submenu_class = ( $depth > 0 ) ? ' dropdown-submenu' : '';
        $output       .= "\n$indent<ul class=\"dropdown-menu$submenu_class\" role=\"menu\">\n";
    }

    public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {

        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $classes   = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'nav-item';

        $has_children = in_array( 'menu-item-has-children', $classes, true );
        if ( $has_children ) {
            $classes[] = 'dropdown';
        }

        $class_names = implode( ' ', array_filter( $classes ) );
        $output     .= $indent . '<li class="' . esc_attr( $class_names ) . '" role="none">';

        /* ── Link ───────────────────────────────────────────────── */
        $atts          = array();
        $atts['href']  = ! empty( $item->url ) ? $item->url : '#';
        $atts['class'] = ( $depth === 0 ) ? 'nav-link' : 'dropdown-item';
        $atts['role']  = 'menuitem';

        if ( $item->target )     { $atts['target'] = $item->target; }
        if ( $item->xfn )        { $atts['rel']    = $item->xfn; }
        if ( $item->attr_title ) { $atts['title']  = $item->attr_title; }

        /* Top-level parent: aria attributes on the <a> */
        if ( $has_children && $depth === 0 ) {
            $atts['aria-haspopup'] = 'true';
            $atts['aria-expanded'] = 'false';
        }

        /* ── FIX: Nested parent – aria only, no data-bs-toggle on <a> ── */
        /* Previously had data-bs-toggle="dropdown" which broke keyboard nav */
        if ( $has_children && $depth > 0 ) {
            $atts['aria-haspopup'] = 'true';
            $atts['aria-expanded'] = 'false';
        }

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            $attributes .= ' ' . $attr . '="' . esc_attr( $value ) . '"';
        }

        $item_output  = isset( $args->before ) ? $args->before : '';

        /*
         * Wrap nav-link + toggle button in a flex container
         * for BOTH depth 0 AND nested parents (depth > 0)
         */
        if ( $has_children ) {
            $item_output .= '<span class="mohi-nav-wrap">';
        }

        $item_output .= '<a' . $attributes . '>';
        $item_output .= isset( $args->link_before ) ? $args->link_before : '';
        $item_output .= apply_filters( 'the_title', $item->title, $item->ID );
        $item_output .= isset( $args->link_after ) ? $args->link_after : '';
        $item_output .= '</a>';

        /*
         * Toggle button for depth 0 (top-level)
         * Caret icon: down arrow
         */
        if ( $has_children && $depth === 0 ) {
            $item_output .= '<button'
                . ' class="mohi-submenu-toggle"'
                . ' type="button"'
                . ' aria-expanded="false"'
                . ' aria-haspopup="true"'
                . ' aria-label="' . esc_attr(
                    sprintf(
                        /* translators: %s: menu item title */
                        esc_html__( 'Open submenu of %s', 'mohi' ),
                        $item->title
                    )
                ) . '"'
                . '>'
                . '<span aria-hidden="true" class="bi bi-caret-down-fill" style="font-size:1em;"></span>'
                . '</button>';
        }

        /*
         * ── FIX: Toggle button for depth > 0 (nested parents) ──
         * Caret icon: right arrow to indicate fly-out direction
         * Same structure as depth 0 button so JS handles it uniformly
         */
        if ( $has_children && $depth > 0 ) {
            $item_output .= '<button'
                . ' class="mohi-submenu-toggle mohi-nested-toggle"'
                . ' type="button"'
                . ' aria-expanded="false"'
                . ' aria-haspopup="true"'
                . ' aria-label="' . esc_attr(
                    sprintf(
                        /* translators: %s: menu item title */
                        esc_html__( 'Open submenu of %s', 'mohi' ),
                        $item->title
                    )
                ) . '"'
                . '>'
                . '<span aria-hidden="true" class="bi bi-caret-right-fill" style="font-size:1em;"></span>'
                . '</button>';
        }

        /* Close mohi-nav-wrap for all parents */
        if ( $has_children ) {
            $item_output .= '</span>'; /* close mohi-nav-wrap */
        }

        $item_output .= isset( $args->after ) ? $args->after : '';
        $output      .= $item_output;
    }

    public function end_el( &$output, $item, $depth = 0, $args = null ) {
        $output .= "</li>\n";
    }

    public function end_lvl( &$output, $depth = 0, $args = null ) {
        $indent  = str_repeat( "\t", $depth );
        $output .= "$indent</ul>\n";
    }
}