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

<c:if test="<%= hasPermission %>">
	<div id="item-finder-root-channel"></div>

	<aui:script require="commerce-frontend-js/components/item_finder/entry as itemFinder, commerce-frontend-js/utilities/slugify as slugify, commerce-frontend-js/utilities/eventsDefinitions as events, commerce-frontend-js/ServiceProvider/index as ServiceProvider">
		var CommerceDiscountChannelsResource = ServiceProvider.default.AdminPricingAPI(
			'v2'
		);

		var id = <%= commerceDiscountId %>;
		var discountExternalReferenceCode =
			'<%= HtmlUtil.escapeJS(commerceDiscount.getExternalReferenceCode()) %>';

		function selectItem(channel) {
			var channelData = {
				channelExternalReferenceCode: channel.externalReferenceCode,
				channelId: channel.id,
				discountExternalReferenceCode: discountExternalReferenceCode,
				discountId: id,
			};

			return CommerceDiscountChannelsResource.addDiscountChannel(id, channelData)
				.then(() => {
					Liferay.fire(events.UPDATE_DATASET_DISPLAY, {
						id:
							'<%= CommercePricingFDSNames.DISCOUNT_QUALIFIER_CHANNELS %>',
					});
				})
				.catch((error) => {
					return Promise.reject(error.title);
				});
		}

		function getSelectedItems() {
			return Promise.resolve([]);
		}

		itemFinder.default('itemFinder', 'item-finder-root-channel', {
			apiUrl: '/o/headless-commerce-admin-channel/v1.0/channels',
			getSelectedItems: getSelectedItems,
			inputPlaceholder: '<%= LanguageUtil.get(request, "find-a-channel") %>',
			itemSelectedMessage: '<%= LanguageUtil.get(request, "channel-selected") %>',
			linkedDatasetsId: [
				'<%= CommercePricingFDSNames.DISCOUNT_QUALIFIER_CHANNELS %>',
			],
			itemCreation: false,
			itemsKey: 'id',
			onItemSelected: selectItem,
			pageSize: 10,
			panelHeaderLabel: '<%= LanguageUtil.get(request, "add-channels") %>',
			portletId: '<%= portletDisplay.getRootPortletId() %>',
			schema: [
				{
					fieldName: 'name',
				},
			],
			spritemap: '<%= themeDisplay.getPathThemeSpritemap() %>',
			titleLabel: '<%= LanguageUtil.get(request, "add-existing-channel") %>',
		});
	</aui:script>

	<commerce-ui:panel
		bodyClasses="p-0"
		title='<%= LanguageUtil.get(request, "channels") %>'
	>
		<frontend-data-set:headless-display
			apiURL="<%= commerceDiscountQualifiersDisplayContext.getDiscountChannelsAPIURL() %>"
			fdsActionDropdownItems="<%= commerceDiscountQualifiersDisplayContext.getDiscountChannelFDSActionDropdownItems() %>"
			formName="fm"
			id="<%= CommercePricingFDSNames.DISCOUNT_QUALIFIER_CHANNELS %>"
			itemsPerPage="<%= 10 %>"
		/>
	</commerce-ui:panel>
</c:if>