(function($) {

    var paczkomaty_select_type = paczkomaty.settings.select_type ? paczkomaty.settings.select_type : 'select';
    var paczkomaty_lang = paczkomaty.lang ? paczkomaty.lang : null;

    $( 'body' ).on( 'updated_checkout', function() {

		var $paczkomat_select = $('#paczkomat_id');
		var options           = {};

		if (paczkomaty_select_type === 'select2_ajax') {
			options = {
				ajax: {
					url: woocommerce_params.ajax_url,
					dataType: 'json',
					delay: 300,
					type: 'POST',
					data: function (params) {
						return {
							action: 'paczkomaty_search_machines_via_ajax',
							city: params.term,
							security: paczkomaty.ajax_nonce,
							cod: $paczkomat_select.data( 'cod' ),
							end_of_week_collection: $paczkomat_select.data( 'end_of_week_collection' ),
						};
					},
					processResults: function (data, params) {
						return {
							results: data.items,
						};
					},
					cache: true,
				},
				minimumInputLength: 3,
				escapeMarkup: function (markup) {
					return markup;
				},
				allowClear: true,
				placeholder: paczkomaty_lang.placeholder,
				language: {
					inputTooShort: function (args) {
						var remainingChars = args.minimum - args.input.length;
						return paczkomaty_lang.min_chars.replace('%', remainingChars);
					},
					loadingMore: function () {
						return paczkomaty_lang.loading_more;
					},
					noResults: function () {
						return paczkomaty_lang.no_results;
					},
					searching: function () {
						return paczkomaty_lang.searching;
					},
					errorLoading: function () {
						return paczkomaty_lang.error_loading;
					},
				},
			};
		}

		if (paczkomaty_select_type === 'select2' || paczkomaty_select_type === 'select2_ajax') {
			if ($.fn.selectWoo) {
				$paczkomat_select.selectWoo(options);
			} else if ($.fn.select2) {
				$paczkomat_select.select2(options);
			}
		}
	});

	window.easyPackAsyncInit = function () {

		var parcel_locker_only = false;

		function remove_map_element() {
			$( '#widget-modal' ).parent().remove();
		}

		$( document ).on( 'click', '#open-geowidget', function (e) {
			e.preventDefault();

			let $paczkomat_id          = $( '#paczkomat_id' );
			let cod                    = $paczkomat_id.data( 'cod' ) === 'TRUE';
			let end_of_week_collection = $paczkomat_id.data( 'end_of_week_collection' ) === 'TRUE';
			let points_types           = ( cod || end_of_week_collection ) ? ['parcel_locker_only'] : ['parcel_locker', 'parcel_locker_superpop'];
			let easePackParams         = {
				apiEndpoint: paczkomaty.api_endpoint,
				initialZoom: 13,
				display: {
					showTypesFilters: true, showSearchBar: true,
				},
				points: {
					types: points_types,
				},
				map: {
					initialTypes: points_types,
				},
			};

			if ( cod || end_of_week_collection ) {
				if ( ! parcel_locker_only ) {
					remove_map_element();
					parcel_locker_only = true;
				}
			} else {
				if ( parcel_locker_only ) {
					remove_map_element();
					parcel_locker_only = false;
				}
			}
			easyPack.init( easePackParams );

			let width     = $( window ).width();
			let max_width = 1000;
			if (width > max_width) {
				width = max_width;
			}
			let height     = $( window ).height();
			let max_height = 800;
			if (height > max_height) {
				height = max_height;
			}

			function set_parcel_machine_from_selected( point ) {
				let $paczkomat_id = $( '#paczkomat_id' );
				if ($( "#paczkomat_id option[value='" + point.name + "']" ).length === 0) {
					let val   = point.name;
					let label = point.name + ',' + point.address.line1 + ',' + point.address.line2;
					$paczkomat_id.append( '<option value="' + val + '">' + label + '</option>' );
				}
				if (paczkomaty_select_type !== 'select2_ajax') {
					$paczkomat_id.val( point.name );
				}
				$paczkomat_id.val( point.name );
				$paczkomat_id.trigger( 'change' );
			}

			easyPack.modalMap(function (point, modal) {
				modal.closeModal();
				set_parcel_machine_from_selected( point );
			}, {width, height});

			setTimeout(
				function() {
					var map = easyPack.map;
					if ($paczkomat_id.val()) {
						map.searchLockerPoint($paczkomat_id.val());
					} else {
						map.searchLockerPoint('');
						map.searchPlace($('#billing_city').val() + ' ' + $('#billing_address_1').val());
					}
				},
				1000
			);
		});
	};

})(jQuery);

