<?php
if ( ! class_exists( 'Vista_Nav_Walker' ) ) :

	class Vista_Nav_Walker extends Walker_Nav_Menu {

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

			$indent = ( $depth > 0 ) ? 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, true );

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

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

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

			if ( in_array( 'current-menu-item', $classes, true ) ) {
				$atts['aria-current'] = 'page';
			}

			$link_class = 'nl';
			if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-ancestor', $classes, true ) ) {
				$link_class .= ' act';
			}
			if ( $has_children ) {
				$link_class .= ' has-sub';
			}
			$atts['class'] = $link_class;

			$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 );

			$icon = '';
			if ( ! empty( $item->description ) && strpos( $item->description, 'fa-' ) !== false ) {
				$icon = '<i class="' . esc_attr( $item->description ) . '" aria-hidden="true"></i>';
			}

			$item_output  = $args->before ?? '';
			$item_output .= '<a' . $attributes . '>';
			$item_output .= $icon;
			$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 );
		}

		public function start_lvl( &$output, $depth = 0, $args = array() ) {
			$indent  = str_repeat( "\t", $depth );
			$output .= "\n$indent<ul class=\"sub-menu nav-sub\">\n";
		}
	}

endif;
if ( ! class_exists( 'Vista_Mobile_Nav_Walker' ) ) :

	class Vista_Mobile_Nav_Walker extends Walker_Nav_Menu {

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

			$indent       = 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, true );

			$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
			$class_attr  = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
			$item_id     = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
			$id_attr     = $item_id ? ' id="' . esc_attr( $item_id ) . '"' : '';

			$output .= $indent . '<li' . $id_attr . $class_attr . '>';

			// لینک
			$atts = array();
			$atts['href']   = ! empty( $item->url ) ? $item->url : '#';
			$atts['target'] = ! empty( $item->target ) ? $item->target : '';
			$atts['rel']    = ! empty( $item->xfn ) ? $item->xfn : '';
			if ( in_array( 'current-menu-item', $classes, true ) ) {
				$atts['aria-current'] = 'page';
			}
			$link_class = 'mnl';
			if ( $has_children ) {
				$link_class .= ' has-sub';
			}
			$atts['class'] = $link_class;
			$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

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

			$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 ?? '' );
			$item_output .= '</a>';
			if ( $has_children ) {
				$item_output .= '<button class="mobile-sub-toggle" aria-expanded="false" aria-label="' . esc_attr__( 'باز/بستن زیرمنو', 'resume-pro' ) . '">'
					. '<i class="fa-solid fa-chevron-down"></i>'
					. '</button>';
			}

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

		public function start_lvl( &$output, $depth = 0, $args = array() ) {
			$indent  = str_repeat( "\t", $depth );
			$output .= "\n$indent<ul class=\"mobile-sub-menu\">\n";
		}
	}

endif;
