<commit_before>import queryString from 'query-string';
import omit from 'lodash/omit';
import oldLayers from 'data/v2-v3-datasets-layers.json';
import basemaps from 'components/map/basemaps';

const oldLayersAndDatasets = oldLayers.reduce((obj, item) => ({
  ...obj,
  ...item.v2_dataset_id && {
    [item.v2_dataset_id]: item.v3_dataset_id
  },
  ...item.v2_layer_id && {
    [item.v2_layer_id]: item.v3_layer_id
  }
}), {});

export const decodeUrlForState = url => {
  const paramsParsed = {};
  const params = queryString.parse(url);
  Object.keys(params).forEach(key => {
    try {
      paramsParsed[key] = JSON.parse(atob(params[key]));
    } catch (err) {
      paramsParsed[key] = params[key];
    }
  });

  if (paramsParsed.map.basemap && paramsParsed.map.basemap.value === 'planet') {
    paramsParsed.planetNotice = true;
  }

  if (paramsParsed.map) {
    paramsParsed.map = {
      ...paramsParsed.map,
      ...paramsParsed.map.datasets && {
        datasets: paramsParsed.map && paramsParsed.map.datasets.reduce((arr, dataset) => [...arr, {
          ...dataset,
          dataset: oldLayersAndDatasets[dataset.dataset] || dataset.dataset,
          layers: dataset.layers.reduce((lArr, layerId) => [...lArr, oldLayersAndDatasets[layerId] || layerId], [])
        }], [])
      },
      ...paramsParsed.map.basemap && paramsParsed.map.basemap.value === 'planet' && {
        basemap: {
          value: 'landsat',
          year: basemaps.landsat.availableYears.includes(paramsParsed.map.basemap.planetYear) ? paramsParsed.map.basemap.planetYear : basemaps.landsat.defaultYear
        }
      }
    };
  }

  return paramsParsed;
};

export const encodeStateForUrl = params => {
  const paramsParsed = {};
  Object.keys(params).forEach(key => {
    if (typeof params[key] === 'object') {
      paramsParsed[key] = btoa(JSON.stringify(params[key]));
    } else {
      paramsParsed[key] = params[key];
    }
  });
  return queryString.stringify(paramsParsed);
};

export const setComponentStateToUrl = ({ key, subKey, change, state }) => {
  const { location: { query, payload, type } } = state();
  let params = change;
  if (query && query[subKey || key] && !!change && typeof change === 'object') {
    params = {
      ...query[subKey || key],
      ...change
    };
  }

  // if a false value is sent we should remove the key from the url
  const cleanLocationQuery =
    !change && query ? omit(query, [subKey || key]) : query;

  return {
    key,
    type,
    payload,
    query: {
      ...cleanLocationQuery,
      ...(params && {
        [subKey || key]: params
      })
    }
  };
};
<commit_msg>check for basemap in params when parsing query<commit_after>import queryString from 'query-string';
import omit from 'lodash/omit';
import oldLayers from 'data/v2-v3-datasets-layers.json';
import basemaps from 'components/map/basemaps';

const oldLayersAndDatasets = oldLayers.reduce((obj, item) => ({
  ...obj,
  ...item.v2_dataset_id && {
    [item.v2_dataset_id]: item.v3_dataset_id
  },
  ...item.v2_layer_id && {
    [item.v2_layer_id]: item.v3_layer_id
  }
}), {});

export const decodeUrlForState = url => {
  const paramsParsed = {};
  const params = queryString.parse(url);
  Object.keys(params).forEach(key => {
    try {
      paramsParsed[key] = JSON.parse(atob(params[key]));
    } catch (err) {
      paramsParsed[key] = params[key];
    }
  });

  if (paramsParsed.map && paramsParsed.map.basemap && paramsParsed.map.basemap.value === 'planet') {
    paramsParsed.planetNotice = true;
  }

  if (paramsParsed.map) {
    paramsParsed.map = {
      ...paramsParsed.map,
      ...paramsParsed.map.datasets && {
        datasets: paramsParsed.map && paramsParsed.map.datasets.reduce((arr, dataset) => [...arr, {
          ...dataset,
          dataset: oldLayersAndDatasets[dataset.dataset] || dataset.dataset,
          layers: dataset.layers.reduce((lArr, layerId) => [...lArr, oldLayersAndDatasets[layerId] || layerId], [])
        }], [])
      },
      ...paramsParsed.map.basemap && paramsParsed.map.basemap.value === 'planet' && {
        basemap: {
          value: 'landsat',
          year: basemaps.landsat.availableYears.includes(paramsParsed.map.basemap.planetYear) ? paramsParsed.map.basemap.planetYear : basemaps.landsat.defaultYear
        }
      }
    };
  }

  return paramsParsed;
};

export const encodeStateForUrl = params => {
  const paramsParsed = {};
  Object.keys(params).forEach(key => {
    if (typeof params[key] === 'object') {
      paramsParsed[key] = btoa(JSON.stringify(params[key]));
    } else {
      paramsParsed[key] = params[key];
    }
  });
  return queryString.stringify(paramsParsed);
};

export const setComponentStateToUrl = ({ key, subKey, change, state }) => {
  const { location: { query, payload, type } } = state();
  let params = change;
  if (query && query[subKey || key] && !!change && typeof change === 'object') {
    params = {
      ...query[subKey || key],
      ...change
    };
  }

  // if a false value is sent we should remove the key from the url
  const cleanLocationQuery =
    !change && query ? omit(query, [subKey || key]) : query;

  return {
    key,
    type,
    payload,
    query: {
      ...cleanLocationQuery,
      ...(params && {
        [subKey || key]: params
      })
    }
  };
};

{'fraction_non_alphanumeric': 0.16198378276447295, 'fraction_numerical': 0.003017160098057703, 'mean_word_length': 2.8574545454545452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1765743', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1777, 'n_words': 328}