<?php
/**
 * The7 nav menu walker class.
 *
 * @since 7.6.0
 *
 * @package The7
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class The7_Nav_Menu_Walker
 */
class The7_Walker_Nav_Menu extends Walker_Nav_Menu {

	protected $dt_menu_parents = array();
	protected $dt_is_first = true;

	function start_lvl( &$output, $depth = 0, $args = array() ) {
		$this->dt_is_first = true;

		$output .= apply_filters( 'presscore_nav_menu_start_lvl', '<ul class="' . esc_attr( $args->submenu_class ) . '">', $depth, $args );
	}

	function end_lvl( &$output, $depth = 0, $args = array() ) {
		$output .= apply_filters( 'presscore_nav_menu_end_lvl', '</ul>', $depth, $args );
	}

	function start_el( &$output, $item, $depth = 0, $args = array(), $current_id = 0 ) {
		$item->dt_is_first = $this->dt_is_first;
		$item->dt_is_parent = in_array( $item->ID, $this->dt_menu_parents );
		$item->dt_is_clickable = ( ! $item->dt_is_parent || $args->parent_is_clickable );

		do_action( 'presscore_nav_menu_start_el', $item, $args, $depth );

		$classes = $this->dt_get_item_classes( $item, $args, $depth );

		$link_before = apply_filters( 'presscore_nav_menu_link_before', $args->link_before, $item, $args, $depth );
		$link_after = apply_filters( 'presscore_nav_menu_link_after', $args->link_after, $item, $args, $depth );
		$el_before = apply_filters( 'presscore_nav_menu_el_before', '', $item, $args, $depth );

		// li wrap
		$output .= $el_before . '<li class="' . implode( ' ', array_filter( $classes ) ) . '">';

		$title = apply_filters( 'the_title', $item->title, $item->ID );
		$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
		$description = ( $item->description ? '<span class="subtitle-text">' . esc_html( $item->description ) . '</span>' : '' );

		$menu_item = $link_before . '<span class="menu-item-text"><span class="menu-text">' . $title . '</span>' . $description . '</span>' . $link_after;

		$attributes = $this->dt_prepare_atts( $this->dt_get_item_atts( $item, $args, $depth ) );
		if ( $attributes ) {
			$item_output = '<a' . $attributes . '>' . $menu_item . '</a>';
		} else {
			$item_output = '<span>' . $menu_item . '</span>';
		}

		$item_output = $args->before . $item_output . $args->after;

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

	function end_el( &$output, $item, $depth = 0, $args = array() ) {
		$this->dt_is_first = false;
		$el_after = apply_filters( 'presscore_nav_menu_el_after', '', $item, $args, $depth );

		$output .= '</li>' . $el_after . ' ';
	}

	function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
		if ( ! $element ) {
			return;
		}

		$id_field = $this->db_fields['id'];
		$id       = $element->$id_field;

		if ( ! empty( $children_elements[ $id ] ) ) {
			$this->dt_menu_parents[] = $id;
		}

		parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
	}

	function dt_get_item_atts( $item, $args, $depth, $atts = array() ) {
		$atts['href'] = ! empty( $item->url ) ? $item->url : '';
		$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
		$atts['target'] = ! empty( $item->target ) ? $item->target : '';
		$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
		$atts['class'] = '';

		// is clickable
		if ( ! $item->dt_is_clickable ) {
			$atts['class'] .= 'not-clickable-item';
		}

		$atts['data-level'] = absint( $depth ) + 1;

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

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

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

		return $attributes;
	}

	function dt_get_item_classes( $item, $args, $depth ) {
		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;

		if (
			in_array( 'current-menu-item',  $classes ) ||
			in_array( 'current-menu-parent',  $classes ) ||
			in_array( 'current-menu-ancestor',  $classes )
		) {
			$classes[] = 'act';
		}

		if ( $item->dt_is_first ) {
			$classes[] = 'first';
		}

		if ( $item->dt_is_parent ) {
			$classes[] = 'has-children';
		}

		$classes = apply_filters( 'presscore_nav_menu_css_class', $classes, $item, $args, $depth );

		return apply_filters( 'nav_menu_css_class', array_unique( $classes ), $item, $args, $depth );
	}
}
