<?php

namespace Linguator\Modules\Blocks;

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

/**
 * @package Linguator
 */

/**
 * Language switcher block for navigation.
 *
 */
class Linguator_Navigation_Language_Switcher_Block extends Linguator_Abstract_Language_Switcher_Block {
	/**
	 * Placeholder used to add language name or flag after WordPress renders the link labels.
	 *
	 * @var string
	 */
	const PLACEHOLDER = '%lmat%';

	/**
	 * Adds the required hooks specific to the navigation language switcher.
	 *
	 *
	 * @return self
	 */
	public function init() {
		parent::init();

		add_action( 'rest_api_init', array( $this, 'linguator_register_switcher_menu_item_options_meta_rest_field' ) );
		add_filter( 'block_type_metadata', array( $this, 'linguator_register_custom_attributes' ) );
		add_filter( 'render_block_core/navigation-link', array( $this, 'linguator_render_custom_attributes' ), 10, 3 );
		add_filter( 'render_block_core/navigation-submenu', array( $this, 'linguator_render_custom_attributes' ), 10, 3 );

		return $this;
	}

	/**
	 * Returns the navigation language switcher block name with the Linguator's namespace.
	 *
	 *
	 * @return string The block name.
	 */
	protected function get_block_name() {
		return 'linguator/navigation-language-switcher';
	}

	/**
	 * Returns the supported pieces of context for the 'linguator/navigation-language-switcher' block.
	 * This context will be inherited from the 'core/navigation' block.
	 *
	 *
	 * @return string[]
	 */
	protected function get_context() {
		return array(
			'textColor',
			'customTextColor',
			'backgroundColor',
			'customBackgroundColor',
			'overlayTextColor',
			'customOverlayTextColor',
			'overlayBackgroundColor',
			'customOverlayBackgroundColor',
			'fontSize',
			'customFontSize',
			'showSubmenuIcon',
			'maxNestingLevel',
			'openSubmenusOnClick',
			'style',
			'isResponsive', // Backward compatibility.
		);
	}

	/**
	 * Renders the `linguator/navigation-language-switcher` block on server.
	 *

	 *
	 * @param array    $attributes The block attributes.
	 * @param string   $content The saved content. Unused.
	 * @param \WP_Block $block The parsed block.
	 * @return string The HTML string output to serve.
	 */
	public function render( $attributes, $content, $block ) {
		$attributes        = $this->set_attributes_for_block( $attributes );
		$switcher          = new \Linguator\Includes\Controllers\Linguator_Switcher();
		$switcher_elements = (array) $switcher->the_languages( $this->links, array_merge( $attributes, array( 'raw' => true ) ) );

		if ( empty( $switcher_elements ) ) {
			return '';
		}

		if ( $attributes['dropdown'] ) {
			$inner_nav_link_blocks = array();
			$top_level_lang        = reset( $switcher_elements );
			foreach ( $switcher_elements as $switcher_element ) {
				$nav_link_block_args = array(
					'blockName' => 'core/navigation-link',
					'attrs'     => $this->linguator_get_core_block_attributes( $attributes, $switcher_element ),
				);

				$inner_nav_link_blocks[] = new \WP_Block( $nav_link_block_args, $block->context );

				if ( $switcher_element['current_lang'] && ! $attributes['hide_current'] ) {
					$top_level_lang = $switcher_element;
				}
			}

			$attributes               = $this->linguator_get_core_block_attributes( $attributes, $top_level_lang );
			$attributes['className'] .= ' ' . wp_apply_generated_classname_support( $block->block_type )['class'];
			$submenu_block_args       = array(
				'blockName'   => 'core/navigation-submenu',
				'attrs'       => $attributes,
				'innerBlocks' => $inner_nav_link_blocks,
			);

			$submenu_block = new \WP_Block( $submenu_block_args, $block->context );
			$output        = $submenu_block->render();
		} else {
			$output = '';

			foreach ( $switcher_elements as $switcher_element ) {
				$link_attributes               = $this->linguator_get_core_block_attributes( $attributes, $switcher_element );
				$link_attributes['className'] .= ' ' . wp_apply_generated_classname_support( $block->block_type )['class'];
				$nav_link_block_args = array(
					'blockName' => 'core/navigation-link',
					'attrs'     => $link_attributes,
				);

				$link_block  = new \WP_Block( $nav_link_block_args, $block->context );
				$output     .= $link_block->render();
			}
		}

		// Final sanitization to ensure flag/name/output content is safe.
		$output = wp_kses( (string) $output, $this->get_allowed_switcher_html() );

		if ( version_compare( $GLOBALS['wp_version'], '6.5-alpha', '<' ) ) {
			/*
			 * Backward compatibility with WordPress < 6.5.
			 * Since WordPress 6.5, our block is rendered automatically inside the auto-generated `<ul>` wrapper.
			 */
			return wp_kses(
				sprintf(
				'<ul class="wp-block-navigation__container wp-block-navigation">%s</ul>',
				$output
				),
				$this->get_allowed_switcher_html()
			);
		}

		return $output;
	}

	/**
	 * Register switcher menu item meta options as a REST API field.
	 *
	 *
	 * @return void
	 */
	public function linguator_register_switcher_menu_item_options_meta_rest_field() {
		register_post_meta(
			'nav_menu_item',
			'_lmat_menu_item',
			array(
				'object_subtype' => 'nav_menu_item',
				'description'    => __( 'Language switcher settings', 'translate-words' ),
				'single'         => true,
				'auth_callback'  => function( $allowed, $meta_key, $post_id ) {
					return current_user_can( 'edit_theme_options' );
				},
				'show_in_rest'   => array(
					'schema' => array(
						'type'                 => 'object',
						'additionalProperties' => array(
							'type' => 'boolean',
						),
					),
				),
			)
		);
	}

	/**
	 * Filters core/navigation-link and core/navigation-submenu attributes during registration to add our own.
	 *
	 *
	 * @param array $metadata Metadata for registering a block type.
	 *
	 * @return array The filtered metadata if about a core/navigation-link.
	 */
	public function linguator_register_custom_attributes( $metadata ) {
		if ( 'core/navigation-link' === $metadata['name'] || 'core/navigation-submenu' === $metadata['name'] ) {
			$lmat_attributes = array(
				'hreflang'       => array(
					'type' => 'string',
				),
				'lang'           => array(
					'type' => 'string',
				),
				'lmat_show_flags' => array(
					'type' => 'boolean',
				),
				'lmat_show_names' => array(
					'type' => 'boolean',
				),
				'lmat_flag'       => array(
					'type' => 'string',
				),
				'lmat_name'       => array(
					'type' => 'string',
				),
			);
			$metadata['attributes'] = array_merge( $metadata['attributes'], $lmat_attributes );
		}

		return $metadata;
	}

	/**
	 * Renders a core/naviagation-link or core/naviagation-submenu block by adding hreflang and lang attributes to the <a> tag
	 * and also the language flag if required.
	 *
	 *
	 * @param string   $block_content The block content.
	 * @param array    $block         The full block, including name and attributes.
	 * @param \WP_Block $instance      The block instance.
	 *
	 * @return string A formatted HTML string representing the core/navigation-link or core/navigation-submenu block.
	 */
	public function linguator_render_custom_attributes( $block_content, $block, $instance ) {
		if ( ! isset(
			$instance->attributes['lmat_show_flags'],
			$instance->attributes['lmat_show_names'],
			$instance->attributes['lmat_flag'],
			$instance->attributes['lmat_name'],
			$instance->attributes['lang'],
			$instance->attributes['hreflang']
		)
		) {
			return $block_content;
		}

		$content_tags = new \WP_HTML_Tag_Processor( $block_content );

		if ( 'core/navigation-submenu' === $instance->name ) {
			// If `openSubmenusOnClick`, the submenu is rendered as a button, so there are no `<a>` to process.
			if ( empty( $instance->context['openSubmenusOnClick'] ) && $content_tags->next_tag( array( 'tag_name' => 'a' ) ) ) {
				$content_tags->set_attribute( 'hreflang', sanitize_text_field( (string) $instance->attributes['hreflang'] ) );
				$content_tags->set_attribute( 'lang', sanitize_text_field( (string) $instance->attributes['lang'] ) );
			}
			if ( $content_tags->next_tag( array( 'tag_name' => 'button' ) ) ) {
				$content_tags->set_attribute(
					'aria-label',
					str_replace(
						static::PLACEHOLDER,
						__( 'Languages', 'translate-words' ),
						(string) $content_tags->get_attribute( 'aria-label' )
					)
				);
			}
		} elseif ( $content_tags->next_tag( array( 'tag_name' => 'a' ) ) ) {
			$content_tags->set_attribute( 'hreflang', sanitize_text_field( (string) $instance->attributes['hreflang'] ) );
			$content_tags->set_attribute( 'lang', sanitize_text_field( (string) $instance->attributes['lang'] ) );
		}

		$overridden_block_content = $content_tags->get_updated_html();

		$link_label = '';

		if ( $instance->attributes['lmat_show_flags'] ) {
			$link_label .= wp_kses( (string) $instance->attributes['lmat_flag'], $this->get_allowed_switcher_html() );
		}

		if ( $instance->attributes['lmat_show_names'] ) {
			$name = esc_html( (string) $instance->attributes['lmat_name'] );
			$link_label .= $instance->attributes['lmat_show_flags'] ? ' ' . $name : $name;
		}

		return wp_kses(
			str_replace(
			static::PLACEHOLDER,
			$link_label,
			$overridden_block_content
			),
			$this->get_allowed_switcher_html()
		);
	}

	/**
	 * Returns attributes that fit for core/navigation-link or core/navigation-submenu and specific to linguator/navigation-language-switcher.
	 *
	 *
	 * @param array $attributes    Array of linguator/navigation-language-switcher attributes.
	 * @param array $switcher_item Array of a switcher item data.
	 * @return array Attributes to be rendered by core.
	 */
	private function linguator_get_core_block_attributes( $attributes, $switcher_item ) {
		return array(
			'label'          => static::PLACEHOLDER,
			'url'            => esc_url_raw( (string) $switcher_item['url'] ),
			'lmat_show_flags' => $attributes['show_flags'],
			'lmat_show_names' => $attributes['show_names'],
			'lang'           => sanitize_text_field( (string) $switcher_item['locale'] ),
			'hreflang'       => sanitize_text_field( (string) $switcher_item['locale'] ),
			'lmat_flag'       => $switcher_item['flag'],
			'lmat_name'       => $switcher_item['name'],
			'className'      => trim( implode( ' ', array_map( 'sanitize_html_class', (array) $switcher_item['classes'] ) ) ),
		);
	}
}

