var { from, fromEvent, EMPTY, forkJoin } = rxjs;
var { fromFetch } = rxjs.fetch;
var { ajax } = rxjs.ajax;
var {
  catchError,
  debounceTime,
  distinctUntilChanged,
  filter,
  map,
  mergeMap,
  switchMap,
  tap,
  delay,
} = rxjs.operators;

var completeResultArray = [];
function getResponse(url, authUserObject) {
  let locationData$;
  if(typeof window.locker !== 'undefined'){
    locationData$ = from(window.locker);
  } else {
    locationData$ = from(fetchLockerListLink()).pipe(
      mergeMap((locationObject) => {
        return locationObject;
      }),
    );
  }

  return locationData$.pipe(
    filter((locationObject) => {
      if (shipany_setting.shipany_locker_include_macuo != null && shipany_setting.shipany_locker_include_macuo == 'no') {
        if (locationObject.addr.cnty === 'MAC') {
          return ''
        }
      }
      if (_locale === "zh") {
        locationObject.addr.reg = translateRegion(locationObject.addr.reg)
        locationObject.addr.state = translateDistrict(locationObject.addr.state)
        locationObject.addr.distr = translateArea(locationObject.addr.distr)
      }

      //NEW CODE
      if (_locale === "en") {
        //districtfilterdata
        districtFilterData.push("All Districts");
      } else if (_locale === "zh") {
        districtFilterData.push("全部地區");
      }

      //NEW CODE
      if (_locale === "en") {
        districtFilterData.push("All Districts");
      } else if (_locale === "zh") {
        districtFilterData.push("全部地區");
      }
      if(region_idx == '2'){
        districtFilterData.push(locationObject.addr.distr);
      }else{
        districtFilterData.push(locationObject.addr.state);
      }

      //NEW CODE
      if (_locale === "en") {
        regionFilterData.push("All Regions");
      } else if (_locale === "zh") {
        regionFilterData.push("全部區域");
      }
      if(region_idx == '2'){
        regionFilterData.push(locationObject.addr.city);
      }else{
        regionFilterData.push(locationObject.addr.reg);
      }

      if (_locale === "en") {
        areaFilterData.push("All Areas");
      } else if (_locale === "zh") {
        areaFilterData.push("全部範圍");
      }
      if(region_idx == '2'){
        // pass
      } else {
        areaFilterData.push(locationObject.addr.distr);
      }
      //NEW CODE

      return locationObject;
    }),
    catchError((err) => {
      // Network or other error, handle appropriately
      console.error(err);
      return of({ error: true, message: err.message });
    })
  );
}

function capitaliseString(str) {
  var res = str.split("_");
  var first = res[0][0].toUpperCase() + res[0].slice(1).toLowerCase();
  var last = res[1][0].toUpperCase() + res[1].slice(1).toLowerCase();

  return first + " " + last;
}
