<?php
/**
 * Custom Navigation Walker for Blog Over
 *
 * Extends Walker_Nav_Menu to add:
 * - ARIA attributes for accessibility
 * - Dropdown toggle buttons
 * - Multi-level support (4 levels)
 * - Custom classes for styling
 *
 * @package Blog_Over
 */

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

class Blog_Over_Nav_Walker extends Walker_Nav_Menu
{

    /**
     * Starts the list before the elements are added.
     */
    public function start_lvl(&$output, $depth = 0, $args = null)
    {
        if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = str_repeat($t, $depth);

        $classes = array('blog-over-sub-menu');

        if ($depth === 0) {
            $classes[] = 'blog-over-dropdown';
        } elseif ($depth === 1) {
            $classes[] = 'blog-over-dropdown-level-2';
        } elseif ($depth >= 2) {
            $classes[] = 'blog-over-dropdown-level-3';
        }

        $class_names = implode(' ', apply_filters('nav_menu_submenu_css_class', $classes, $args, $depth));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';

        $output .= "{$n}{$indent}<ul{$class_names} aria-hidden=\"true\">{$n}";
    }

    /**
     * Ends the list after elements are added.
     */
    public function end_lvl(&$output, $depth = 0, $args = null)
    {
        if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = str_repeat($t, $depth);
        $output .= "$indent</ul>{$n}";
    }

    /**
     * Starts the element output.
     */
    public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
    {
        if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = ($depth) ? str_repeat($t, $depth) : '';

        $classes = empty($item->classes) ? array() : (array) $item->classes;
        $classes[] = 'blog-over-menu-item';
        $classes[] = 'blog-over-menu-item-' . $item->ID;

        if ($depth === 0) {
            $classes[] = 'blog-over-menu-item-top';
        } else {
            $classes[] = 'blog-over-menu-item-child';
        }

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

        if ($has_children) {
            $classes[] = 'blog-over-has-dropdown';
        }

        $args = apply_filters('nav_menu_item_args', $args, $item, $depth);

        $class_names = implode(' ', 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', 'blog-over-menu-item-' . $item->ID, $item, $args, $depth);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';

        // ARIA attributes for parent items
        $aria_attrs = '';
        if ($has_children) {
            $aria_attrs = ' aria-haspopup="true" aria-expanded="false"';
        }

        $output .= $indent . '<li' . $id . $class_names . $aria_attrs . '>';

        $atts = array();
        $atts['title'] = !empty($item->attr_title) ? $item->attr_title : '';
        $atts['target'] = !empty($item->target) ? $item->target : '';
        if ('_blank' === $item->target && empty($item->xfn)) {
            $atts['rel'] = 'noopener';
        } else {
            $atts['rel'] = $item->xfn;
        }
        $atts['href'] = !empty($item->url) ? $item->url : '';
        $atts['aria-current'] = $item->current ? 'page' : '';

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

        $attributes = '';
        foreach ($atts as $attr => $value) {
            if (is_scalar($value) && '' !== $value && false !== $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 . ' class="blog-over-menu-link">';
        $item_output .= $args->link_before . $title . $args->link_after;
        $item_output .= '</a>';

        // Add dropdown toggle button for items with children
        if ($has_children) {
            $item_output .= '<button class="blog-over-dropdown-toggle" aria-label="' . esc_attr__('Expand child menu', 'blog-over') . '" aria-expanded="false">';
            $item_output .= '<span class="blog-over-dropdown-icon"></span>';
            $item_output .= '</button>';
        }

        $item_output .= $args->after;

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

    /**
     * Ends the element output.
     */
    public function end_el(&$output, $item, $depth = 0, $args = null)
    {
        if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
            $n = '';
        } else {
            $n = "\n";
        }
        $output .= "</li>{$n}";
    }
}
