<%--
/**
 * 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.
 *
 *
 *
 */
--%>

<%
Hits hits = searchDisplayContext.getHits();
%>

<clay:row
	cssClass='<%= "search-layout" + (searchDisplayContext.isShowMenu() ? " menu-column" : StringPool.BLANK) %>'
>
	<c:if test="<%= searchDisplayContext.isShowMenu() %>">
		<clay:col
			id='<%= liferayPortletResponse.getNamespace() + "facetNavigation" %>'
			md="3"
		>

			<%
			for (SearchFacet searchFacet : ListUtil.sort(searchDisplayContext.getEnabledSearchFacets(), SearchFacetComparator.INSTANCE)) {
				if (searchFacet.isStatic()) {
					continue;
				}

				request.setAttribute("search.jsp-facet", searchFacet.getFacet());

				searchFacet.includeView(request, PipingServletResponseFactory.createPipingServletResponse(pageContext));
			}
			%>

		</clay:col>
	</c:if>

	<clay:col
		cssClass="result"
		md="<%= searchDisplayContext.isShowMenu() ? String.valueOf(9) : String.valueOf(12) %>"
	>
		<%@ include file="/main_search_suggest.jspf" %>

		<liferay-ui:search-container
			id="search"
			searchContainer="<%= searchDisplayContext.getSearchContainer() %>"
		>
			<liferay-ui:search-container-row
				className="com.liferay.portal.kernel.search.Document"
				escapedModel="<%= false %>"
				keyProperty="UID"
				modelVar="document"
				stringKey="<%= true %>"
			>

				<%
				com.liferay.portal.search.web.internal.result.display.context.builder.SearchResultSummaryDisplayContextBuilder searchResultSummaryDisplayContextBuilder = new com.liferay.portal.search.web.internal.result.display.context.builder.SearchResultSummaryDisplayContextBuilder();

				searchResultSummaryDisplayContextBuilder.setAssetEntryLocalService(com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil.getService());
				searchResultSummaryDisplayContextBuilder.setCurrentURL(currentURL);
				searchResultSummaryDisplayContextBuilder.setDocument(document);
				searchResultSummaryDisplayContextBuilder.setFastDateFormatFactory(com.liferay.portal.kernel.util.FastDateFormatFactoryUtil.getFastDateFormatFactory());
				searchResultSummaryDisplayContextBuilder.setGroupLocalService(com.liferay.portal.kernel.service.GroupLocalServiceUtil.getService());
				searchResultSummaryDisplayContextBuilder.setHighlightEnabled(searchDisplayContext.isHighlightEnabled());
				searchResultSummaryDisplayContextBuilder.setLanguage(com.liferay.portal.kernel.language.LanguageUtil.getLanguage());
				searchResultSummaryDisplayContextBuilder.setLocale(locale);
				searchResultSummaryDisplayContextBuilder.setObjectDefinitionLocalService(com.liferay.object.service.ObjectDefinitionLocalServiceUtil.getService());
				searchResultSummaryDisplayContextBuilder.setPortletURLFactory(searchDisplayContext.getPortletURLFactory());
				searchResultSummaryDisplayContextBuilder.setRenderRequest(renderRequest);
				searchResultSummaryDisplayContextBuilder.setRenderResponse(renderResponse);
				searchResultSummaryDisplayContextBuilder.setRequest(request);
				searchResultSummaryDisplayContextBuilder.setResourceActions(com.liferay.portal.kernel.security.permission.ResourceActionsUtil.getResourceActions());
				searchResultSummaryDisplayContextBuilder.setSearchResultPreferences(searchDisplayContext.getSearchResultPreferences());
				searchResultSummaryDisplayContextBuilder.setSummaryBuilderFactory(searchDisplayContext.getSummaryBuilderFactory());
				searchResultSummaryDisplayContextBuilder.setThemeDisplay(themeDisplay);
				searchResultSummaryDisplayContextBuilder.setUserLocalService(com.liferay.portal.kernel.service.UserLocalServiceUtil.getService());

				com.liferay.portal.search.web.internal.result.display.context.SearchResultSummaryDisplayContext searchResultSummaryDisplayContext = searchResultSummaryDisplayContextBuilder.build();
				%>

				<c:if test="<%= searchResultSummaryDisplayContext != null %>">
					<%@ include file="/main_search_result_form.jspf" %>
				</c:if>
			</liferay-ui:search-container-row>

			<liferay-ui:search-iterator
				displayStyle="descriptive"
				markupView="lexicon"
				type="more"
			/>

			<c:if test="<%= searchDisplayContext.isDisplayMainQuery() && (searchDisplayContext.getQueryString() != null) %>">
				<div class="full-query">
					<code>
						<%= HtmlUtil.escape(searchDisplayContext.getQueryString()) %>
					</code>
				</div>
			</c:if>
		</liferay-ui:search-container>
	</clay:col>
</clay:row>

<aui:script require="frontend-js-web/index as frontendJsWeb">
	var {delegate} = frontendJsWeb;

	var facetNavigation = document.getElementById(
		'<portlet:namespace />facetNavigation'
	);

	if (facetNavigation) {
		delegate(facetNavigation, 'click', '.facet-value a', (event) => {
			event.preventDefault();

			var target = event.delegateTarget;

			if (!target.classList.contains('active')) {
				target.classList.add('active');

				var searchFacet = target.closest('.search-facet');

				var facetValueSiblings = Array.prototype.filter.call(
					target.parentNode.children,
					(child) => {
						return child !== target;
					}
				);

				facetValueSiblings.forEach((facet) => {
					facet.classList.remove('active');
				});

				var form = document.<portlet:namespace />fm;

				var field = Liferay.Util.getFormElement(
					form,
					searchFacet.dataset.facetfieldname
				);

				if (field) {
					field.value = target.dataset.value;

					var fieldSelection = Liferay.Util.getFormElement(
						form,
						searchFacet.dataset.facetfieldname + 'selection'
					);

					if (fieldSelection) {
						fieldSelection.value = target.dataset.selection;
					}

					Liferay.Util.postForm(form, {
						data: {
							<%= SearchContainer.DEFAULT_CUR_PARAM %>: 1,
						},
					});
				}
			}
		});
	}

	var searchResultsContainer = document.getElementById(
		'<portlet:namespace />search'
	);

	if (searchResultsContainer) {
		delegate(searchResultsContainer, 'click', '.expand-details', (event) => {
			var target = event.delegateTarget;

			var targetSiblings = Array.prototype.filter.call(
				target.parentNode.children,
				(child) => {
					return (
						child !== target &&
						child.classList.contains('table-details')
					);
				}
			);

			targetSiblings.forEach((sibling) => {
				if (sibling.classList.contains('hide')) {
					sibling.classList.remove('hide');
				}
				else {
					sibling.classList.add('hide');
				}
			});
		});
	}
</aui:script>