<%--
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<div class="search-suggested-spelling">
	<c:if test="<%= searchDisplayContext.isCollatedSpellCheckResultEnabled() && !Validator.isBlank(hits.getCollatedSpellCheckResult()) && !Objects.equals(hits.getCollatedSpellCheckResult(), searchDisplayContext.getKeywords()) %>">
		<ul class="list-inline suggested-keywords">
			<li class="label label-default">
				<liferay-ui:message key="did-you-mean" />:
			</li>
			<li>

				<%
				String taglibSearchCollatedKeywords = liferayPortletResponse.getNamespace() + "searchKeywords('" + HtmlUtil.escapeJS(hits.getCollatedSpellCheckResult()) + "')";
				%>

				<aui:a href="javascript:;" onclick="<%= taglibSearchCollatedKeywords %>">

					<%
					KeywordsSuggestionHolder keywordsSuggestionHolder = new KeywordsSuggestionHolder(hits.getCollatedSpellCheckResult(), searchDisplayContext.getKeywords());

					for (String suggestedKeyword : keywordsSuggestionHolder.getSuggestedKeywords()) {
					%>

						<span class="<%= keywordsSuggestionHolder.hasChanged(suggestedKeyword) ? "changed-keyword" : "unchanged-keyword" %>">
							<%= HtmlUtil.escape(suggestedKeyword) %>
						</span>

					<%
					}
					%>

				</aui:a>
			</li>
		</ul>
	</c:if>

	<c:if test="<%= searchDisplayContext.isQuerySuggestionEnabled() && ArrayUtil.isNotEmpty(hits.getQuerySuggestions()) %>">
		<ul class="list-inline related-queries">
			<li class="label label-default">
				<liferay-ui:message key="related-queries" />:
			</li>

			<%
			for (String querySuggestion : hits.getQuerySuggestions()) {
			%>

				<c:if test="<%= !Validator.isBlank(querySuggestion) && !querySuggestion.equals(searchDisplayContext.getKeywords()) %>">
					<li>

						<%
						String taglibOnClick = liferayPortletResponse.getNamespace() + "searchKeywords('" + HtmlUtil.escapeJS(querySuggestion) + "')";
						%>

						<aui:a href="javascript:;" onClick="<%= taglibOnClick %>">

							<%
							KeywordsSuggestionHolder keywordsSuggestionHolder = new KeywordsSuggestionHolder(querySuggestion, searchDisplayContext.getKeywords());

							for (String suggestedKeyword : keywordsSuggestionHolder.getSuggestedKeywords()) {
							%>

								<span class="<%= keywordsSuggestionHolder.hasChanged(suggestedKeyword) ? "changed-keyword" : "unchanged-keyword" %>">
									<%= HtmlUtil.escape(suggestedKeyword) %>
								</span>

							<%
							}
							%>

						</aui:a>
					</li>
				</c:if>

			<%
			}
			%>

		</ul>
	</c:if>
</div>

<script>
	function <portlet:namespace />searchKeywords(newKeywords) {
		Liferay.Util.postForm(document.<portlet:namespace />fm, {
			data: {
				keywords: newKeywords,
			},
		});
	}
</script>