<?php
/**
 * Bootstrap 5 Nav Walker
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

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

/**
 * Custom Nav Walker for Bootstrap 5
 */
class Paws_And_Care_Bootstrap_Nav_Walker extends Walker_Nav_Menu {

    /**
     * Start Level
     */
    public function start_lvl( &$output, $depth = 0, $args = null ) {
        $indent = str_repeat( "\t", $depth );
        $output .= "\n$indent<ul class=\"dropdown-menu\">\n";
    }

    /**
     * Start Element
     */
    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';

        // Check if item has children
        $has_children = in_array( 'menu-item-has-children', $classes, true );

        if ( $has_children ) {
            // Level 0 is standard dropdown, deeper levels are dropend (side menu)
            $classes[] = ( $depth === 0 ) ? 'dropdown' : 'dropend';
        }

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $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 : '';

        // Add nav-link class
        if ( $depth === 0 ) {
            $atts['class'] = 'nav-link';
        } else {
            $atts['class'] = 'dropdown-item';
        }

        // Add active class
        if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-ancestor', $classes, true ) ) {
            $atts['class'] .= ' active';
        }

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

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

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

        $item_output  = $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . $title . $args->link_after;
        
        // Add chevron inside the link for items with children
        if ( $has_children ) {
            $item_output .= ' <span class="dropdown-toggle-icon" role="button" tabindex="0" aria-expanded="false" aria-label="' . esc_attr__( 'Toggle submenu', 'paws-and-care' ) . '"><i class="fa-solid fa-chevron-down"></i></span>';
        }
        
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $args );
    }

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

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

    /**
     * Fallback function when no menu is set
     */
    public static function fallback( $args ) {
        if ( current_user_can( 'edit_theme_options' ) ) {
            echo '<ul class="' . esc_attr( $args['menu_class'] ) . '">';
            echo '<li class="nav-item">';
            echo '<a class="nav-link" href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">' . esc_html__( 'Set Up Menu', 'paws-and-care' ) . '</a>';
            echo '</li>';
            echo '</ul>';
        }
    }
}

