jQuery( document ).ready( function() {
	var newOption = jQuery( "<option selected='selected'></option>" )
		.val( elexFilterData.vendor_id )
		.text( elexFilterData.vendor_name );

	jQuery( '.elex-admin-add-vendor-filter-product-vendor-name' ).append( newOption ).trigger( 'change' );

	jQuery( '.elex-admin-add-vendor-filter-product-vendor-name' ).select2( {
		minimumInputLength: 2,
		placeholder:        'Vendor Name',
		ajax: {
			url:  elexFilterData.ajax_url,
			type: 'GET',
			data: function( params ) {
				return {
					vendor_search: params.term,
					status:        'publish',
					action:        'elex_mv_api_endpoint_for_vendors',
					nonce:         elexFilterData.nonce,
				};
			},
			processResults: function( response ) {
				return {
					results: response.data.map( function( store ) {
						return {
							text: store.post_title,
							id:   store.ID,
						};
					} ),
				};
			},
		},
	} );
} );
