(function( $ ) {
	const mapEnable 			= FFLDELAERSCRIPT.map_enable;
	const activation_key 		= FFLDELAERSCRIPT.apiKey;
	const gKey 					= FFLDELAERSCRIPT.gKey;
	const message_body 			= FFLDELAERSCRIPT.message_body;
	const modalEnable 			= FFLDELAERSCRIPT.modalEnable
	const hook 					= FFLDELAERSCRIPT.hook;
	const rest_url 				= FFLDELAERSCRIPT.rest_url;
	const icon 					= FFLDELAERSCRIPT.icon;
	const search_button 		= FFLDELAERSCRIPT.search_button;
	const select_dealer_button 	= FFLDELAERSCRIPT.select_dealer_button;
	const defaultDealers 		= FFLDELAERSCRIPT.default_dealers;
	


	localStorage.removeItem("selectedFFL");

	if(!document.getElementById("ffl-dealer-zip-code")) {
		 document.addEventListener("DOMContentLoaded", function() {
			 initFFLJs();
		 });
	 }
	function initFFLJs() {
		if(mapEnable ==1){
			FFL.init({
				container : 'ffl-dealer-container',
				apiKey: activation_key,
				geoKey: gKey,
				cBack : getSelected,
				icon_url: icon,
				mapEnable: mapEnable
			});
		}
	}
	
	if(modalEnable){
		jQuery(document).on('click','.modal-close',function () {
			jQuery("#ffl-dealer-modal").css('display','none');
		});
	}

	jQuery('.woocommerce-shipping-fields__field-wrapper').find('input').val(null);

	jQuery('form.woocommerce-checkout').on('checkout_place_order',
		function(e) {
			if(localStorage.getItem("selectedFFL") === null) {
				jQuery("#ffl-dealer-modal").css('display','block');
				e.preventDefault();
				e.stopPropagation();
				return false;
			} else {
				if(jQuery('.woocommerce-error').length === 0) {
					document.getElementById("ship-to-different-address-checkbox").disabled = false;
					document.getElementById("shipping_country").disabled = false;
					document.getElementById("shipping_state").disabled = false;
				}
			}
	} );


	jQuery("#shipping_country").val(null).trigger('change');
	jQuery("#order_comments").val(null).text(null);
	if(message_body.heading){
		jQuery('.ffl-dealer-heading').html(message_body.heading);
	}
	if(message_body.message){
		jQuery('.notice').html(message_body.message);
	}
	 if (typeof jQuery('#modal-reopen') !== 'undefined' && jQuery('#modal-reopen').length) {
		 jQuery('#modal-reopen').on('click',function(){
			jQuery("#ffl-dealer-modal").css('display','block'); 
		 });
	 }

	if(search_button.button_text){
		jQuery("#ffl-search").text(search_button.button_text);
	}
	if(select_dealer_button.button_text){
		jQuery(".dealer-button").text(select_dealer_button.button_text);
	}
	
	function getSelected(data) {

		jQuery("#shipping_country").prop("disabled",false);
		jQuery("#shipping_state").prop("disabled",false);

		jQuery("#shipping_country").val("US"); // Change the value or make some change to the internal state
		jQuery("#shipping_country").trigger("change");
		jQuery("#shipping_country").prop("disabled",true);

		var elemsActive = document.querySelectorAll(".selectedFFLDivButton");

		[].forEach.call(elemsActive, function(el) {
			el.classList.remove("selectedFFLDivButton");
		});

		document.getElementById(data.license_number).childNodes[0].classList.add('selectedFFLDivButton');
		localStorage.setItem("selectedFFL",data.license_number);
		
		document.getElementById( "place_order" ).disabled = false
		
		let business_name = data.business_name?data.business_name:data.license_name;
		var company = document.getElementsByName("shipping_company")[0];
		setNativeValue(company,business_name);
		company.readOnly = true;

		var address1 = document.getElementsByName("shipping_address_1")[0];
		setNativeValue(address1,data.premise_street);
		address1.readOnly = true;

		var city = document.getElementsByName("shipping_city")[0];
		setNativeValue(city,data.premise_city);
		city.readOnly = true;
		
		var city = document.getElementsByName("shipping_city")[0];
		setNativeValue(city,data.premise_city);
		city.readOnly = true;
		
		var postalCode = document.getElementsByName("shipping_postcode")[0];
		setNativeValue(postalCode,data.premise_zip_code);
		postalCode.readOnly = true;

		var orderComment = document.getElementsByName("order_comments")[0];
		var orderCommentText = 'FFL Number : ' + data.license_number + ' - ' +
			'FFL : ' + business_name + ' - ' +
			'Street : ' + data.premise_street + ' - ' +
			'City : ' + data.premise_city + ' - ' +
			'State : ' + data.premise_state + ' - ' +
			'Zip and Phone: ' + data.premise_zip_code + ' - ' + data.voice_phone;

		orderComment.readOnly = true;
		setNativeValue(orderComment,orderCommentText);
		
		jQuery("#shipping_state").val(data.premise_state);
		
		
		jQuery("#shipping_state option[value='"+data.premise_state+"']").prop('selected', true);
		jQuery("#shipping_state").trigger("change");
		jQuery("#shipping_country").prop("disabled",true);
		//jQuery("#shipping_state").prop("disabled",true);
		jQuery('#selected-notification').notify("Dealer has been added as a shipping address", "success",{ position:"left" });
		return false;
	}

	function setNativeValue(element, value) {
		var _ref = Object.getOwnPropertyDescriptor(element, 'value') || {},
			valueSetter = _ref.set;

		var prototype = Object.getPrototypeOf(element);

		var _ref2 = Object.getOwnPropertyDescriptor(prototype, 'value') || {},
			prototypeValueSetter = _ref2.set;

		if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
			prototypeValueSetter.call(element, value);
		} else if (valueSetter) {
			valueSetter.call(element, value);
		} else {}
	}
})( jQuery );
