<?php
/**
 * Menu walker for header dropdown menus.
 * 
 * @deprecated since 1.5.0
 */
namespace Greyd\Navigation;

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

class Dropdown_Menu_Walker extends \Walker_Nav_Menu {

	public $output_started = false;

	// Don't start or end the top level
	function start_lvl( &$output, $depth = 0, $args = array() ) {
		if ( $depth == 0 ) {
			return;
		} else {
			parent::start_lvl( $output, $depth, $args );
		}
	}
	function end_lvl( &$output, $depth = 0, $args = array() ) {
		if ( $depth == 0 ) {
			return;
		} else {
			parent::end_lvl( $output, $depth, $args );
		}
	}

	// print dropdowns
	function start_el( &$output, $item, $depth = 0, $args = array(), $current_object_id = 0 ) {
		if ( $depth == 0 ) {
			
			// debug($item);
			// debug($args);
			$tab = $args->tab;

			$this->output_started = false;

			if ( ! empty( $item->sub_id ) ) {
				// // print top-level dropdowns
				// $this->output_started = true;

				// $output .= "<div id='" . $item->sub_id . "_content' class='dropmenu_content submenu'><div class='dropmenu_tab " . $tab . "'></div><ul>";

			}
			elseif ( ! empty( $item->template ) ) {
				// print template dropdowns
				$tmp   = explode( '|', $item->template );
				$slug  = $tmp[0];
				$width = $tmp[1];
				if ( $width == '' || $width == 'auto' ) {
					$width = 'max-content';
				}
				if ( $width == 'full' ) {
					$width = '100%';
				}
				if ( $width == 'custom' ) {
					$width = $tmp[2];
					if ( strpos( $width, '%' ) === false && strpos( $width, 'px' ) === false ) {
						$width = (int) $width;
						if ( $width < 100 ) {
							$width .= '%';
						} else {
							$width .= 'px';
						}
					}
				}
				$output .= "<div id='" . $item->ID . "_content' class='dropmenu_content template' style='width: " . $width . ";'>
								<div class='dropmenu_tab " . $tab . "'></div>
								<div class='dropmenu_template'>" . do_shortcode( "[dynamic template='" . $slug . "']" ) . '</div>
							</div>';
			}
			return;
		}
		parent::start_el( $output, $item, $depth, $args, $current_object_id );
	}
	function end_el( &$output, $item, $depth = 0, $args = array() ) {
		if ( $depth == 0 ) {
			if ( $this->output_started ) {
				$output .= '</ul></div>';
				$this->output_started = false;
			}
			return;
		}
		parent::end_el( $output, $item, $depth, $args );
	}

	// Only print sub-level elements
	function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
		// debug($element);
		if ( intval( $depth ) > 0 ) {
			if ( ! $this->output_started ) {
				return;
			}
		}
		parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
	}
}
