<?php

/**
 * Helper | Menu
 *
 * @package Dotspice
 * @version 1.3.0
 */


/**
 * Navbar menu button HTML
 */
function dotspice_navbar_menu_btn($theme_location = 'primary')
{
?>
	<div class="navbar-btn__wrap menu">
		<div class="navbar-btn navbar-btn__menu collapsed" data-target="navbar-nav__<?php echo esc_attr($theme_location); ?>">
			<span></span><span></span><span></span>
		</div>
		<div class="navbar-overlay double" data-target="navbar-nav__<?php echo esc_attr($theme_location); ?>">
			<div class="navbar-btn navbar-btn__menu">
				<span></span><span></span><span></span>
			</div>
		</div>
	</div>
	<?php
}


/**
 * Navbar content button HTML
 */
function dotspice_navbar_content_btn($button_name = '')
{
	$button_group = dotspice_get_theme_option('header_button_' . $button_name);

	if (!empty($button_group['enable'])) {
		$add_class   = dotspice_convert_dashes($button_name);
		$button_icon = $button_group['icon'];
		$button_tag  = 'div';
		$button_attr = '';

		if ($button_name == 'account' && class_exists('woocommerce')) {
			$button_tag  = 'a';
			$button_attr = sprintf(' href="%s"', get_permalink(get_option('woocommerce_myaccount_page_id')));
		}

	?>
		<div class="navbar-btn__wrap">
			<<?php echo esc_attr($button_tag); ?><?php echo wp_kses($button_attr, 'dotspice-tags') ?> class="navbar-btn <?php echo esc_attr($add_class); ?>">
				<?php

				if (substr($button_icon, 0, 1) === '%') {
					switch ($button_icon) {
						case '%lang_text%':
							dotspice_languages_list('lang_text');
							break;
						case '%lang_image%':
							dotspice_languages_list('lang_image');
							break;
					}
				} else {
					echo dotspice_get_svg_icon(esc_attr($button_icon));
				}

				if ($button_name == 'mini_cart' && class_exists('woocommerce')) {
					$cart_count = WC()->cart->get_cart_contents_count();

					echo sprintf('<span class="count" data-count="%s">%s</span>', esc_attr($cart_count), esc_html($cart_count));
				}
				?>
			</<?php echo esc_attr($button_tag); ?>>

			<?php if (!in_array($button_name, array('account'))) { ?>
				<div class="navbar-btn__content <?php echo esc_attr($add_class); ?>">
					<?php
					switch ($button_name) {
						case 'search_posts':
							get_search_form();
							break;
						case 'languages':
							dotspice_languages_list();
							break;
						case 'search_products':
							if (class_exists('woocommerce')) {
								get_product_search_form();
							}
							break;
						case 'mini_cart':
							if (class_exists('woocommerce')) {
					?><div class="widget_shopping_cart_content"><?php woocommerce_mini_cart(); ?></div><?php
																										}
																										break;
																								}
																											?>
				</div>
			<?php } ?>
		</div>
<?php
	}
}


/**
 * nav_menu_link_attributes | Bootstrap nav-link
 */
function dotspice_nav_menu_link_attributes($atts, $item, $args, $depth)
{
	$atts['class'] = 'nav-link';

	if (in_array($args->theme_location, array('primary', 'additional'))) {
		if (in_array('click', $item->classes)) {
			$atts['aria-haspopup'] = 'true';
			$atts['aria-expanded'] = 'false';
			$atts['data-toggle']   = 'dropdown';
		}
	}

	return $atts;
}
add_filter('nav_menu_link_attributes', 'dotspice_nav_menu_link_attributes', 10, 4);


/**
 * wp_nav_menu | Bootstrap sub-menu
 */
function dotspice_wp_nav_menu($nav_menu, $args)
{
	if (in_array($args->theme_location, array('primary', 'additional'))) {
		$nav_menu = preg_replace('/class="sub-menu"/', 'class="dropdown-menu"', $nav_menu);
		$nav_menu = preg_replace('/menu-item-has-children/', 'dropdown', $nav_menu);
		$nav_menu = preg_replace('/menu-item/', 'nav-item', $nav_menu);
	}

	return $nav_menu;
}
add_filter('wp_nav_menu', 'dotspice_wp_nav_menu', 10, 2);


/**
 * walker_nav_menu_start_el | sub-menu Icons
 */
function dotspice_walker_nav_menu_start_el($item_output, $item, $depth, $args)
{
	if (in_array($args->theme_location, array('primary', 'additional'))) {

		// Depth Icon
		$depth_icon = '';

		if (in_array('menu-item-has-children', (array) $item->classes)) {
			if ($depth == 0) {
				$depth_icon = dotspice_get_svg_icon('angle-down');
			} else {
				$depth_icon = dotspice_get_svg_icon('angle-right');
			}
		}

		$item_output = preg_replace('/(<\/(a|div)>)/is', $depth_icon . '$1', trim($item_output));
	}

	return $item_output;
}
add_filter('walker_nav_menu_start_el', 'dotspice_walker_nav_menu_start_el', 10, 4);


/**
 * wp_nav_menu_items | Allow shortcode
 */
add_filter('wp_nav_menu_items', 'do_shortcode');
