(function ($){
"use strict";
if(typeof realhomesLocationsData!=='undefined'){
const hierarchicalLocations=realhomesLocationsData.all_locations;    
const selectBoxesIDs=realhomesLocationsData.select_names;               
const selectBoxesCount=parseInt(realhomesLocationsData.select_count);   
const multiSelect=realhomesLocationsData.multi_select_locations;        
const anyText=realhomesLocationsData.any_text;      /* "Any" text as it could be translated */
const anyValue=realhomesLocationsData.any_value;    /* "any" value */
const slugsInQueryParams=realhomesLocationsData.locations_in_params;    
const consoleLogEnabled=false; 
if(consoleLogEnabled){
console.log('realhomesLocationsData.locations_in_params: ');
console.log(slugsInQueryParams);
}
(function (){
prepareSelectBoxes();
let parentLocations=[];
for (let selectIndex=0; selectIndex < selectBoxesCount; selectIndex++){
const currentSelect=$('#' + selectBoxesIDs[selectIndex]); 
const currentIsLast=(selectBoxesCount===(selectIndex + 1)); 
if(selectIndex===0){ 
parentLocations=addParentLocations(currentSelect, currentIsLast);
}else{ 
if(parentLocations.length > 0){
let currentLocations=[];
const previousSelect=$('#' + selectBoxesIDs[selectIndex - 1]);
if(previousSelect.val()===anyValue){
for (let i=0; i < parentLocations.length; i++){
let tempLocations=addChildrenLocations(currentSelect, parentLocations[i].children, '', currentIsLast);
if(tempLocations.length > 0){
currentLocations=$.merge(currentLocations, tempLocations);
}}
}else{
let previousLocation=searchLocation(previousSelect.val(), hierarchicalLocations);
if(previousLocation&&previousLocation.children.length > 0){
currentLocations=addChildrenLocations(currentSelect, previousLocation.children, '', currentIsLast);
}}
previousSelect.change(updateChildSelect);
parentLocations=currentLocations;
}}
if(parentLocations.length===0){
disableSelect(currentSelect);
break;
}else{
selectParamOption(currentSelect);
}}   
})(); 
function addParentLocations(targetSelect, addAllChildren){
let addedLocations=[];
let insertionCounter=0;
hierarchicalLocations.forEach(function (currentLocation, index, locationsArray){
targetSelect.append('<option value="' + currentLocation.slug + '">' + currentLocation.name + '</option>');
addedLocations[insertionCounter++]=currentLocation;
if(consoleLogEnabled){
console.log('addParentLocations: ' + currentLocation.slug + ' in ' + targetSelect.attr('id'));
}
if(addAllChildren&&currentLocation.children.length){
addChildrenLocations(targetSelect, currentLocation.children, '- ', addAllChildren);
}});
return addedLocations;
}
function addChildrenLocations(targetSelect, childrenLocations, prefix, addAllChildren){
let addedChildrenLocations=[];
let insertionCounter=0;
childrenLocations.forEach(function (currentLocation, index, locationsArray){
targetSelect.append('<option value="' + currentLocation.slug + '">' + prefix + currentLocation.name + '</option>');
addedChildrenLocations[insertionCounter++]=currentLocation;
if(consoleLogEnabled){
console.log(prefix + 'addChildrenLocations: ' + currentLocation.slug + ' in ' + targetSelect.attr('id'));
}
if(addAllChildren&&currentLocation.children.length){
let tempLocations=addChildrenLocations(targetSelect, currentLocation.children, prefix + '- ', addAllChildren);
if(tempLocations.length > 0){
addedChildrenLocations=$.merge(addedChildrenLocations, tempLocations);
}}
});
return addedChildrenLocations;
}
function searchLocation(slug, locations){
let targetLocation=false;
for (let index=0; index < locations.length; index++){
let currentLocation=locations[index];
if(currentLocation.slug===slug){
if(consoleLogEnabled){
console.log('searchLocation: Found');
console.log(currentLocation);
}
targetLocation=currentLocation;
break;
}
if(currentLocation.children.length > 0){
targetLocation=searchLocation(slug, currentLocation.children);
if(targetLocation){
break;
}}
}
return targetLocation;
}
function updateChildSelect(event){
let selectedSlug=$(this).val();
let currentSelectIndex=selectBoxesIDs.indexOf($(this).attr('id'));
if(consoleLogEnabled){
console.log('updateChildSelect: ' + $(this).attr('id') + ' select box is changed to ' + selectedSlug + ' slug ');
}
if(selectedSlug===anyValue&&(currentSelectIndex > -1)&&(currentSelectIndex < (selectBoxesCount - 1))){
for (let s=currentSelectIndex; s < (selectBoxesCount - 1); s++){
let childSelectIsLast=(selectBoxesCount===(s + 2));
let childSelect=$('#' + selectBoxesIDs[s + 1]);
childSelect.empty();
addAnyOption(childSelect);
let anyChildLocations=[];
$('#' + selectBoxesIDs[s] + ' > option').each(function (){
if(this.value!==anyValue){
let relatedLocation=searchLocation(this.value, hierarchicalLocations);
if(relatedLocation&&relatedLocation.children.length > 0){
let tempChildrenLocations=addChildrenLocations(childSelect, relatedLocation.children, '', childSelectIsLast);
if(tempChildrenLocations.length > 0){
anyChildLocations=$.merge(anyChildLocations, tempChildrenLocations);
}}
}});
if(anyChildLocations.length > 0){
enableSelect(childSelect);
}else{
disableSelect(childSelect);
break;
}}   
}else{
let selectedParentLocation=searchLocation(selectedSlug, hierarchicalLocations);
if(selectedParentLocation){
let childLocations=[];
for (let childSelectIndex=currentSelectIndex + 1; childSelectIndex < selectBoxesCount; childSelectIndex++){
let childSelectIsLast=(selectBoxesCount===(childSelectIndex + 1));
let childSelect=$('#' + selectBoxesIDs[childSelectIndex]);
childSelect.empty();
if(childLocations.length===0){
if(selectedParentLocation.children.length > 0){
addAnyOption(childSelect);
let tempLocations=addChildrenLocations(childSelect, selectedParentLocation.children, '', childSelectIsLast);
if(tempLocations.length > 0){
childLocations=tempLocations;
}}
}else if(childLocations.length > 0){ 
let currentLocations=[];
for (let i=0; i < childLocations.length; i++){
let tempChildLocation=childLocations[i];
if(tempChildLocation.children.length > 0){
addAnyOption(childSelect);
let tempLocations=addChildrenLocations(childSelect, tempChildLocation.children, '', childSelectIsLast);
if(tempLocations.length > 0){
currentLocations=$.merge(currentLocations, tempLocations);
}}
}
childLocations=currentLocations;
}
if(childLocations.length > 0){
enableSelect(childSelect);
}else{
disableSelect(childSelect);
break;
}} 
}else{
if(consoleLogEnabled){
console.log('updateChildSelect: Not Found ' + selectedSlug + ' slug in hierarchicalLocations!');
console.log(hierarchicalLocations);
}}
}}
function addAnyOption(targetSelect){
if(targetSelect.has('option').length > 0){
return;
}
let targetSelectIndex=selectBoxesIDs.indexOf(targetSelect.attr('id'));    
if(targetSelect.parents('.rh_prop_search__select').hasClass('rh_location_prop_search_' + targetSelectIndex)){
let targetSelectPlaceholder=targetSelect.parents('.rh_prop_search__select').data('get-location-placeholder');
targetSelect.append('<option value="' + anyValue + '" selected="selected">' + targetSelectPlaceholder + '</option>');
if(consoleLogEnabled){
console.log('addAnyOption: to select box: ' + targetSelect.attr('id'));
}
}else if(targetSelect.parents('.rh_prop_loc__select').hasClass('rh_location_prop_loc_' + targetSelectIndex)){
targetSelect.append('<option value="' + anyValue + '" selected="selected">' + anyText + '</option>');
if(consoleLogEnabled){
console.log('addAnyOption: to select box: ' + targetSelect.attr('id'));
}}
}
function disableSelect(targetSelect){
let targetSelectID=targetSelect.attr('id');
if(consoleLogEnabled){
console.log('disableSelect: ' + targetSelectID);
}
targetSelect.empty();
targetSelect.closest('.option-bar').addClass('disabled');
if(targetSelect.is(':enabled')){
targetSelect.prop('disabled', true);
targetSelect.parents('.rh_prop_search__select').addClass('rh_disable_parent');
}
let targetSelectIndex=selectBoxesIDs.indexOf(targetSelectID);
let nextSelectBoxesCount=selectBoxesCount - (targetSelectIndex + 1);
if(nextSelectBoxesCount > 0){
let nextSelect=$('#' + selectBoxesIDs[targetSelectIndex + 1]);
disableSelect(nextSelect);
}}
function enableSelect(targetSelect){
let targetSelectID=targetSelect.attr('id');
if(consoleLogEnabled){
console.log('enableSelect: ' + targetSelectID);
}
if(targetSelect.is(':disabled')){
targetSelect.prop('disabled', false);
}
targetSelect.parents('.rh_prop_search__select').map(function (){
if($(this).hasClass('rh_disable_parent')){
$(this).removeClass('rh_disable_parent');
}});
let optionWrapper=targetSelect.closest('.option-bar');
if(optionWrapper.hasClass('disabled')){
optionWrapper.removeClass('disabled');
}
}
function selectParamOption(currentSelect){
if(Object.keys(slugsInQueryParams).length > 0){
let selectName=currentSelect.attr('name');
selectName=selectName.replace(/[\[\]]+/g,''); 
if(typeof slugsInQueryParams[selectName]!=='undefined'){
let tempValue=slugsInQueryParams[selectName];
if(Array.isArray(tempValue)){
for (let i=0; i < tempValue.length; i++){
currentSelect.find('option[value="' + tempValue[i] + '"]').prop('selected', true);
}}else{
currentSelect.find('option[value="' + tempValue + '"]').prop('selected', true);
}}
}}
function prepareSelectBoxes(){
for (let selectIndex=0; selectIndex < selectBoxesCount; selectIndex++){
let currentSelectId=selectBoxesIDs[selectIndex];
let currentSelect=$('#' + currentSelectId);
if((multiSelect==='no') &&
(currentSelect.has('option').length===0) &&
(currentSelect.parents('.rh_prop_search__select').hasClass('rh_location_prop_search_' + selectIndex))){
if(consoleLogEnabled){
console.log('prepareSelectBoxes 1st if: ' + currentSelectId);
}
addAnyOption(currentSelect);
}
if((currentSelect.has('option').length===0) &&
(currentSelect.parents('.rh_prop_loc__select').hasClass('rh_location_prop_loc_' + selectIndex))){
if(consoleLogEnabled){
console.log('prepareSelectBoxes 2nd if: ' + currentSelectId);
}
addAnyOption(currentSelect);
}}
}}
})(jQuery);