const dataMap = {
  all: 'All Civic Space related Incidents',
  covid: 'COVID-9',
  lock: 'Solidarity',
  qua: 'Dialogue',
  rel: 'Safety',
  irr: 'Association',
  pre: 'Assembly',
  hea: 'Expression',
};
function changeData(e) {
  const value = e.value;
  // clearExistingSource()
  if (value === 'covid') {
    $('#barChartData').load('showBarData.php?code=all', {});
  } else {
    //alert(value);
    var pro = $('#province-select').val();
    if (pro == '') {
      $('#barChartData').load('showBarData.php?code=' + value, {});
      showHorizontal(value);
    } else {
      $('#barChartData').load(
        'showBarDataProvinceWise.php?code=' + value + '&value=' + pro,
        {},
      );
    }
    let dataC = $('#data-select').val();
    showCivicIncidents(dataC);
    showSuicideData(dataC);
    dataSelectChange();

    changeBolData();
  }
}

function clearExistingSource() {
  let latlng = [84.124, 28.3949];
  var zoom = 6;
  map.flyTo({
    center: {
      lat: latlng[1],
      lng: latlng[0],
    },
    speed: 0.4,
    zoom: zoom,
  });
  $('#district-select').selectpicker('deselectAll');
  $('#district-select').selectpicker('refresh');
  $('#province-select').selectpicker('deselectAll');
  $('#province-select').selectpicker('refresh');

  let sources = map.getStyle().sources;
  let layers = map.getStyle().layers;
  for (var i in layers) {
    let layer = layers[i];
    var key = Object.keys(layer)[0];
    var ly = layer[key];
    var provLayer = ly.startsWith('prov');
    var distLayer = ly.startsWith('district');
    if (provLayer || distLayer) {
      map.removeLayer(ly);
    }
  }
  Object.keys(sources).map(function (k) {
    var provLayer = k.startsWith('prov');
    var distLayer = k.startsWith('district');
    if (provLayer || distLayer) {
      map.removeSource(k);
    }
  });
}

