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

<%@ include file="/init.jsp" %>

<%
AssetEntriesSearchFacet assetEntriesSearchFacet = (AssetEntriesSearchFacet)request.getAttribute("facet_configuration.jsp-searchFacet");

JSONObject dataJSONObject = assetEntriesSearchFacet.getData();

int frequencyThreshold = dataJSONObject.getInt("frequencyThreshold");

String[] assetTypes = new String[0];

List<KeyValuePair> currentAssetTypes = new ArrayList<KeyValuePair>();

if (dataJSONObject.has("values")) {
	JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");

	assetTypes = new String[valuesJSONArray.length()];

	for (int i = 0; i < valuesJSONArray.length(); i++) {
		assetTypes[i] = valuesJSONArray.getString(i);

		currentAssetTypes.add(new KeyValuePair(assetTypes[i], ResourceActionsUtil.getModelResource(locale, assetTypes[i])));
	}
}

List<KeyValuePair> availableAssetTypes = new ArrayList<KeyValuePair>();

for (AssetRendererFactory<?> assetRendererFactory : assetEntriesSearchFacet.getAssetRendererFactories(company.getCompanyId())) {
	String className = assetRendererFactory.getClassName();

	if (assetRendererFactory.isSearchable() && !ArrayUtil.contains(assetTypes, className)) {
		availableAssetTypes.add(new KeyValuePair(className, ResourceActionsUtil.getModelResource(locale, className)));
	}
}
%>

<aui:input label="frequency-threshold" name='<%= assetEntriesSearchFacet.getClassName() + "frequencyThreshold" %>' value="<%= frequencyThreshold %>" />

<aui:input name='<%= assetEntriesSearchFacet.getClassName() + "assetTypes" %>' type="hidden" />

<liferay-ui:input-move-boxes
	leftBoxName="currentAssetTypes"
	leftList="<%= currentAssetTypes %>"
	leftTitle="current"
	rightBoxName="availableAssetTypes"
	rightList="<%= availableAssetTypes %>"
	rightTitle="available"
/>

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

	var currentAssetTypes = Liferay.Util.getFormElement(form, 'currentAssetTypes');

	if (currentAssetTypes) {
		form.addEventListener('submit', (event) => {
			event.preventDefault();

			var data = {};

			data[
				'<%= assetEntriesSearchFacet.getClassName() + "assetTypes" %>'
			] = Liferay.Util.getSelectedOptionValues(currentAssetTypes);

			Liferay.Util.postForm(form, {data: data});
		});
	}
</script>