<%--
/**
 * 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:void(0);" onclick="<%= taglibSearchCollatedKeywords %>">

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

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

						<span class="<%= keywordsSuggestionHolder.hasChanged(suggestedKeyword) ? "changed-keyword" : "unchanged-keyword" %>">
							<c:choose>
								<c:when test="<%= keywordsSuggestionHolder.hasChanged(suggestedKeyword) %>">
									<strong><%= HtmlUtil.escape(suggestedKeyword) %></strong>
								</c:when>
								<c:otherwise>
									<%= HtmlUtil.escape(suggestedKeyword) %>
								</c:otherwise>
							</c:choose>
						</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:void(0);" onClick="<%= taglibOnClick %>">

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

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

								<span class="<%= keywordsSuggestionHolder.hasChanged(suggestedKeyword) ? "changed-keyword" : "unchanged-keyword" %>">
									<c:choose>
										<c:when test="<%= keywordsSuggestionHolder.hasChanged(suggestedKeyword) %>">
											<strong><%= HtmlUtil.escape(suggestedKeyword) %></strong>
										</c:when>
										<c:otherwise>
											<%= HtmlUtil.escape(suggestedKeyword) %>
										</c:otherwise>
									</c:choose>
								</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>