<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Custom Navigation Walker for Pawfect Theme
 */
class Pawfect_Nav_Walker extends Walker_Nav_Menu
{

    /**
     * Start the list before the elements are added.
     */
    public function start_lvl(&$output, $depth = 0, $args = null)
    {
        $indent = str_repeat("\t", $depth);

        // Different positioning for different depth levels
        if ($depth === 0) {
            // 2nd level - dropdown below parent
            $classes = "pawfect-dropdown-menu pawfect-absolute pawfect-top-full pawfect-left-0 pawfect-bg-pawfect-dark pawfect-rounded-b-md pawfect-shadow-lg pawfect-py-2 pawfect-min-w-[180px] pawfect-z-50 pawfect-opacity-0 pawfect-translate-y-3 pawfect-pointer-events-none pawfect-transition-all pawfect-duration-300";
        }
        else {
            // 3rd level and beyond - cascade to the right
            $classes = "pawfect-dropdown-menu pawfect-absolute pawfect-top-0 pawfect-left-full pawfect-bg-pawfect-dark pawfect-rounded-md pawfect-shadow-lg pawfect-py-2 pawfect-min-w-[180px] pawfect-z-50 pawfect-opacity-0 pawfect-translate-x-2 pawfect-pointer-events-none pawfect-transition-all pawfect-duration-300";
        }

        $output .= "\n$indent<div class=\"$classes\">\n";
        $output .= "\n$indent\t<ul class=\"pawfect-list-none\" role=\"list\">\n";
    }

    /**
     * End the list after the elements are added.
     */
    public function end_lvl(&$output, $depth = 0, $args = null)
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent\t</ul>\n";
        $output .= "$indent</div>\n";
    }

    /**
     * Start the element output.
     */
    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[] = 'menu-item-' . $item->ID;

        $has_children = in_array('menu-item-has-children', $classes);
        $is_current = in_array('current-menu-item', $classes) || in_array('current_page_item', $classes) || in_array('current-menu-ancestor', $classes);

        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));

        if ($depth === 0) {
            $nav_classes = 'pawfect-nav-item pawfect-relative';
            if ($has_children) {
                $nav_classes .= ' pawfect-group pawfect-has-dropdown';
            }
            if ($is_current) {
                $nav_classes .= ' current-menu-item';
            }
            $class_names = $class_names ? ' class="' . $nav_classes . '"' : '';
        }
        else {
            // For submenu items, also add group class if they have children
            $submenu_classes = 'pawfect-submenu-item pawfect-relative';
            if ($has_children) {
                $submenu_classes .= ' pawfect-group pawfect-has-dropdown';
            }
            if ($is_current) {
                $submenu_classes .= ' current-menu-item';
            }
            $class_names = $class_names ? ' class="' . $submenu_classes . '"' : '';
        }

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

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

        $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) . '"' : '';

        if ($depth === 0) {
            $link_class = $has_children ? 'pawfect-nav-link pawfect-inline-flex pawfect-items-center' : 'pawfect-nav-link';
        }
        else {
            $link_class = $has_children ? 'pawfect-dropdown-link pawfect-inline-flex pawfect-items-center pawfect-justify-between' : 'pawfect-dropdown-link';
        }

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

        if ($has_children) {
            // Close the <a> tag first, then add a separate toggle button
            $item_output .= '</a>';

            // Dropdown toggle button — separate from the link so clicking
            // the arrow doesn't navigate. tabindex="-1" removes it from the
            // Tab order (TwentyTwenty pattern) — submenu opens via :focus-within
            // on the parent <li> instead, so Tab goes directly to submenu items.
            $item_output .= '<button type="button" tabindex="-1" class="pawfect-dropdown-toggle pawfect-bg-transparent pawfect-border-0 pawfect-cursor-pointer pawfect-p-0 pawfect-ml-1 pawfect-inline-flex pawfect-items-center" aria-expanded="false" aria-label="' . esc_attr(sprintf(__('Toggle submenu for %s', 'pawfect'), $item->title)) . '">';

            if ($depth === 0) {
                // Down arrow for main menu items
                $item_output .= '<svg class="pawfect-w-2 pawfect-h-2" fill="currentColor" viewBox="0 0 9 9">';
                $item_output .= '<path d="M8.54204 2.55806C8.78612 2.80214 8.78612 3.19786 8.54204 3.44194L4.94242 7.04156C4.82521 7.15877 4.66586 7.225 4.5001 7.225C4.33434 7.225 4.17537 7.15915 4.05816 7.04194L0.458157 3.44194C0.214079 3.19786 0.214079 2.80214 0.458157 2.55806C0.702234 2.31398 1.09796 2.31398 1.34204 2.55806L4.5001 5.71612L7.65816 2.55806C7.90223 2.31398 8.29796 2.31398 8.54204 2.55806Z" fill="currentColor" />';
                $item_output .= '</svg>';
            }
            else {
                // Right arrow for submenu items
                $item_output .= '<svg class="pawfect-w-2 pawfect-h-2" fill="currentColor" viewBox="0 0 9 9">';
                $item_output .= '<path d="M2.55806 0.458157C2.80214 0.214079 3.19786 0.214079 3.44194 0.458157L7.04156 4.05778C7.15877 4.17499 7.225 4.33434 7.225 4.5001C7.225 4.66586 7.15915 4.82483 7.04194 4.94204L3.44194 8.54204C3.19786 8.78612 2.80214 8.78612 2.55806 8.54204C2.31398 8.29796 2.31398 7.90223 2.55806 7.65816L5.71612 4.5001L2.55806 1.34204C2.31398 1.09796 2.31398 0.702234 2.55806 0.458157Z" fill="currentColor" />';
                $item_output .= '</svg>';
            }

            $item_output .= '</button>';
        }
        else {
            $item_output .= '</a>';
        }
        $item_output .= isset($args->after) ? $args->after : '';

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

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

/**
 * Mobile Navigation Walker for Pawfect Theme
 */
class Pawfect_Mobile_Nav_Walker extends Walker_Nav_Menu
{

    /**
     * Start the list before the elements are added.
     */
    public function start_lvl(&$output, $depth = 0, $args = null)
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class=\"pawfect-mobile-submenu pawfect-pl-4 pawfect-mt-1\">\n";
        $output .= "\n$indent\t<div class=\"pawfect-mobile-submenu-list\">\n";
    }

    /**
     * End the list after the elements are added.
     */
    public function end_lvl(&$output, $depth = 0, $args = null)
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent\t</div>\n";
        $output .= "$indent</div>\n";
    }

    /**
     * Start the element output.
     */
    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[] = 'menu-item-' . $item->ID;

        $has_children = in_array('menu-item-has-children', $classes);
        $is_current = in_array('current-menu-item', $classes) || in_array('current_page_item', $classes) || in_array('current-menu-ancestor', $classes);

        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));

        $mobile_classes = 'pawfect-mobile-menu-item pawfect-flex pawfect-items-center pawfect-py-3';
        // Only add bottom border for top-level items
        if ($depth === 0) {
            $mobile_classes .= ' pawfect-border-b pawfect-border-gray-700';
        }
        if ($has_children) {
            $mobile_classes .= ' pawfect-mobile-has-children';
        }
        if ($is_current) {
            $mobile_classes .= ' pawfect-text-pawfect-primary';
        }

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

        $output .= $indent . '<div class="' . $mobile_classes . '"' . $id . '>';

        $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) . '"' : '';

        $item_output = isset($args->before) ? $args->before : '';
        $item_output .= '<a class="pawfect-mobile-menu-link pawfect-text-white pawfect-no-underline pawfect-flex pawfect-items-center pawfect-flex-1"' . $attributes . '>';
        $item_output .= '<span>' . (isset($args->link_before) ? $args->link_before : '') . apply_filters('the_title', $item->title, $item->ID) . (isset($args->link_after) ? $args->link_after : '') . '</span>';
        $item_output .= '</a>';

        if ($has_children) {
            $item_output .= '<button type="button" class="pawfect-mobile-submenu-toggle pawfect-transition-transform pawfect-duration-200 pawfect-bg-transparent pawfect-border-0 pawfect-text-white pawfect-cursor-pointer pawfect-p-2" aria-expanded="false" aria-label="' . esc_attr(sprintf(__('Toggle submenu for %s', 'pawfect'), $item->title)) . '"><svg class="pawfect-w-4 pawfect-h-4" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg></button>';
        }

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

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

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