<?php
/**
 * Navigation Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Navigation_Class as Navigation_Class,
	Errigal_WP_Hook as WP_Hook,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

/**
 * This class is in charge of adding navigation hooks.
 *
 * @since 1.1.0
 */
class Errigal_Navigation_Hooks extends Errigal_Hooks_Base {
	/**
	 * Adds arrow to menu items with sub menus.
	 *
	 * @since 1.0.1
	 *
	 * @link https://developer.wordpress.org/reference/hooks/walker_nav_menu_start_el/
	 *
	 * @param string  $item_output Menu item's starting HTML output.
	 * @param WP_Post $menu_item   Menu item data object.
	 * @return string
	 */
	public function add_arrow_to_menu_items_with_sub_menus( $item_output, $menu_item ) {
		if (
			Validator::is_non_empty_string( $item_output ) &&
			Content::is_wp_post( $menu_item ) &&
			Validator::has_string_list_item( $menu_item->classes, 'menu-item-has-children', true )
		) {
			$html_builder = Html_Builder::create();
			$a_close_tag  = $html_builder->create_a()->get_close_tag();
			$item_output  = str_replace(
				$a_close_tag,
				$html_builder->create_span(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
						),
					)
				)->get_element(
					/* translators: Ensure the screen reader text starts with a full stop (.) in the language for proper reading pause. */
					esc_html__( '. Sub menu is available. Go to the button next to this link to open sub menu.', 'errigal' )
				) .
				$a_close_tag .
				$html_builder->create_button(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME    => 'sub-menu-toggle',
							Html_Attr::TYPE          => Html_Attr::VALUE_BUTTON,
							Html_Attr::ARIA_CONTROLS => '',
							Html_Attr::ARIA_EXPANDED => false,
						),
					)
				)->get_element(
					Icon::get_icon( Icon::DOWN_ARROW, Icon::SIZE_SM ) .
						$html_builder->create_span(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME  => array(
										Navigation_Class::SUB_MENU_TOGGLE_TEXT_OPEN,
										Accessibility_Class::SCREEN_READER_TEXT,
									),
									Html_Attr::ARIA_HIDDEN => false,
								),
							)
						)->get_element(
							esc_html__( 'Open menu', 'errigal' )
						) .
						$html_builder->create_span(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME  => array(
										Navigation_Class::SUB_MENU_TOGGLE_TEXT_CLOSE,
										Accessibility_Class::SCREEN_READER_TEXT,
									),
									Html_Attr::ARIA_HIDDEN => true,
								),
							)
						)->get_element(
							esc_html__( 'Close menu', 'errigal' )
						)
				),
				$item_output
			);
		}

		return $item_output;
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		add_filter( WP_Hook::WALKER_NAV_MENU_START_EL, array( $this, 'add_arrow_to_menu_items_with_sub_menus' ), WP_Hook::PRIORITY_DEFAULT, 2 );
	}
}
