/**
 * UpSolution Element: List Search
 */
! function( $, _undefined ) {
	"use strict";

	const DELETE_FILTER = null;
	const urlManager = $ush.urlManager();
	const urlParam = '_s';

	/**
	 * @param {Node} container.
	 */
	function usListSearch( container ) {
		const self = this;

		// Bindable events
		self._events = {
			searchTextChanged: self.searchTextChanged.bind( self ),
			formSubmit: self.formSubmit.bind( self ),
		};

		// Elements
		self.$container = $( container );
		self.$input = $( 'input', container );
		self.$pageContent = $( 'main#page-content' );
		self.$message = $( '.w-search-message', container );

		// Private "Variables"
		self.name = self.$input.attr( 'name' );
		self.listSelector = $ush.toString( self.$container.data( 'selector' ) ).trim();

		// Sets value from URL
		if ( self.changeURLParams() ) {
			let urlValue = urlManager.get( urlParam );
			if ( ! $ush.isUndefined( urlValue ) ) {
				self.$input.val( urlValue );
			}
		}

		// Events
		self.$container
			.on( 'input', 'input', $ush.throttle( self._events.searchTextChanged, /* wait */300, /* no_trailing */false ) )
			.on( 'click', 'buttom', self._events.searchTextChanged )
			.on( 'submit', 'form', self._events.formSubmit );

		// Defines enter presses in field
		$us.$document.on( 'keypress', ( e ) => {
			if ( self.$input.is( ':focus' ) && e.keyCode === $ush.ENTER_KEYCODE ) {
				e.preventDefault();
				self.searchTextChanged( e );
			}
		});
	}

	// List Search API
	$.extend( usListSearch.prototype, {

		/**
		 * Determines if enabled URL.
		 *
		 * @return {Boolean} True if enabled url, False otherwise.
		 */
		changeURLParams: function() {
			return this.$container.hasClass( 'change_url_params' );
		},

		/**
		 * @event handler
		 * @param {Event} e
		 */
		formSubmit: function( e ) {
			e.preventDefault();
			this.searchTextChanged( e );
		},

		/**
		 * @event handler
		 * @param {Event} e
		 */
		searchTextChanged: function( e ) {
			const self = this;
			var $listToSearch;

			if ( self.listSelector ) {
				$listToSearch = $( self.listSelector, self.$pageContent );
			} else {
				$listToSearch = $( `
				.w-grid.us_post_list:visible,
				.w-grid.us_product_list:visible,
				.w-grid-none:visible
			`, self.$pageContent ).first();
			}

			if ( $listToSearch.hasClass( 'w-grid' ) ) {
				self.$message.addClass( 'hidden' ).text('');
				$listToSearch.addClass( 'used_by_list_search' );

			} else if ( ! $listToSearch.hasClass( 'w-grid-none' ) ) {
				self.$message
					.html( 'No suitable list found. Add <b>Post List</b> or <b>Product List</b> elements.' ) // do not need to translate
					.removeClass( 'hidden' );
			}
			if ( e.type === 'input' && ! self.$container.hasClass( 'live_search' ) ) {
				return;
			}
			let value = self.$input.val();
			if ( value === '' ) {
				value = DELETE_FILTER;
			}
			if ( value === self.lastValue ) {
				return;
			}
			self.lastValue = value;

			if ( self.changeURLParams() ) {
				urlManager.set( urlParam, value ).push();
			}

			$listToSearch.trigger( 'usListSearch', [ urlParam, value ] );
		}

	} );

	$.fn.usListSearch = function() {
		return this.each( function() {
			$( this ).data( 'usListSearch', new usListSearch( this ) );
		} );
	};

	$( () => {
		$( '.w-search.for_list' ).usListSearch();
	} );

}( jQuery );
