<?php
/**
 * Zentorone Bootstrap 5 Nav Walker – Checkbox Hack Multi-level (No JS Dropdown)
 *
 * @package Zentorone
 */

class Zentorone_Bootstrap_Nav_Walker extends Walker_Nav_Menu {

	private $submenu_index = 0;

	/**
	 * Starts the list before the elements are added.
	 */
	public function start_lvl( &$output, $depth = 0, $args = null ) {
        $indent = str_repeat( "\t", $depth );
        $classes = array( 'dropdown-menu' );
        if ( $depth > 0 ) {
            $classes[] = 'shadow-none';
            $classes[] = 'shadow-lg';
        }
        $class_str = implode( ' ', $classes );
        $output .= "\n$indent<ul class=\"$class_str\">\n";
	}

	/**
	 * Starts 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[] = 'nav-item';

        $has_children = $args->walker->has_children;

        if ( $has_children ) {
			$classes[] = 'dropdown';

            // Depth > 0 ise alt menü yönü: admin'de 'dropstart' class'ı varsa sola, yoksa sağa
			if ( $depth > 0 ) {
				$classes[] = 'dropdown-submenu';
                if ( ! in_array( 'dropstart', $classes ) ) {
					$classes[] = 'dropend'; // varsayılan sağa açılan
				}
                // dropstart zaten varsa dokunma
			}
		}

		if ( in_array( 'current-menu-item', $classes ) ) {
			$classes[] = 'active';
		}

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

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

        // Checkbox ID'si – her alt menü için unique
        $checkbox_id = '';
        if ( $has_children ) {
            $this->submenu_index++;
            $checkbox_id = 'submenu-toggle-' . $this->submenu_index;
        }

		$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        : '#';

        // Link class
		$atts['class'] = ( $depth > 0 ) ? 'dropdown-item' : 'nav-link';

        if ( $has_children ) {
            $atts['class'] .= ' dropdown-toggle';
		}

		if ( in_array( 'current-menu-item', $item->classes ) ) {
			$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 );

		$item_output = $args->before;

        if ( $has_children ) {
            // Checkbox + Label yapısı
            $item_output .= '<input type="checkbox" id="' . $checkbox_id . '" class="submenu-checkbox d-none" tabindex="-1" aria-hidden="true">';
		$item_output .= '<a' . $attributes . '>';
            $item_output .= '<span>' . $args->link_before . $title . $args->link_after . '</span>';
            $item_output .= '<label for="' . $checkbox_id . '" class="dropdown-caret">';

			if ( $depth === 0 ) {
                // Ana seviye: aşağı ok
                $item_output .= '<i class="bi bi-chevron-down ms-1"></i>';
			} else {
                // Alt seviyeler: yöne göre
				if ( in_array( 'dropstart', $classes ) ) {
                    $item_output .= '<i class="bi bi-chevron-left me-1"></i>';
				} else {
                    $item_output .= '<i class="bi bi-chevron-right"></i>';
				}
			}

            $item_output .= '</label>';
            $item_output .= '</a>';
        } else {
            // Çocuk yok – normal link
            $item_output .= '<a' . $attributes . '>';
            $item_output .= $args->link_before . $title . $args->link_after;
		$item_output .= '</a>';
        }

		$item_output .= $args->after;

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