<commit_before>import T from "../constants/ActionTypes";
import parseUrl from "../util/parseUrl";
import { MAIN_IDS, IDS } from "../constants/Categories";

const initialState = {
  text: null,
  city: null,
  result: [],
  error: false,
  current: null,
  categories: [],
  highlight: [],
  invisible: [],
  addresses: [],
  cities: [],
  searchByUrl: true,
  showingAllEntries: false,
  moreEntriesAvailable: false
};

const unique = cities =>
  cities
    .map(
      ({city, name, state, country, lat, lon, osm_id}) =>
        ({
          city: (city || name),
          state,
          country,
          lat,
          lon,
          osm_id
        }))
    .filter((item, pos, self) => 
      self.findIndex(x => (
          x.city == item.city
          && x.state == item.state
          && x.country == item.country
        )) == pos)

const isCity = x =>
  (
    (
      (x["class"] === 'place' && (x.type === 'city' || x.type === 'village')) ||
      (x["class"] === 'boundary' && x.type === 'administrative')
    )
  )

module.exports = (state = initialState, action = {}) => {

  switch (action.type) {

    case T.TOGGLE_SEARCH_CATEGORY:
      const c = action.payload;
      if (c == null) {
        return state;
      }
      //TODO: beautify
      const cats = state.categories.indexOf(c) >= 0
        ? (() => {
          var ref1 = state.categories;
          var results = [];
          var x, i, len;
          for (i = 0, len = ref1.length; i < len; i++) {
            x = ref1[i];
            if (x !== c) {
              results.push(x);
            }
          }
          return results;
        })()
      : [c].concat([].slice.call(state.categories));

      return {
        ...state,
        categories: cats
      }

    case T.SET_SEARCH_TEXT:
      return {
        ...state,
        text: action.payload
      }

    case T.UPDATE_STATE_FROM_URL:
      const searchText = parseUrl(action.payload).params.search;

      return {
        ...state,
        text: searchText || ""
      }

    case T.SET_CITY_SEARCH_TEXT:
      return {
        ...state,
        city: action.payload
      }

    case T.SEARCH_RESULT:
      if (!action.error) {
        return {
          ...state,
          result: action.payload.visible,
          invisible: action.payload.invisible
        }
      }
      return state;
      break;

    case T.SEARCH_ADDRESS_RESULT:
      if (!action.error) {
        const d = action.payload || [];
        return {
          ...state,
          addresses: d,
          cities: unique(d.filter(isCity)),
          error: false
        }
      } else {
        return {
          ...state,
          addresses: [],
          cities: [],
          error: true
        }
      }
      break;

    case T.NEW_ENTRY_RESULT:
      if (!action.error) {
        var newResult = state.result;
        newResult.push(action.payload.id);
        return {
          ...state,
          result: newResult
        }
      }
      break;

    case T.SET_CURRENT_ENTRY:
      const p = action.payload;
      if (p != null) {
        return {
          ...state,<commit_msg>fix(osm): fix that pin of new entry is shown again when clicking the save-button<commit_after>import T from "../constants/ActionTypes";
import parseUrl from "../util/parseUrl";
import { MAIN_IDS, IDS } from "../constants/Categories";

const initialState = {
  text: null,
  city: null,
  result: [],
  error: false,
  current: null,
  categories: [],
  highlight: [],
  invisible: [],
  addresses: [],
  cities: [],
  searchByUrl: true,
  showingAllEntries: false,
  moreEntriesAvailable: false
};

const unique = cities =>
  cities
    .map(
      ({city, name, state, country, lat, lon, osm_id}) =>
        ({
          city: (city || name),
          state,
          country,
          lat,
          lon,
          osm_id
        }))
    .filter((item, pos, self) => 
      self.findIndex(x => (
          x.city == item.city
          && x.state == item.state
          && x.country == item.country
        )) == pos)

const isCity = x =>
  (
    (
      (x["class"] === 'place' && (x.type === 'city' || x.type === 'village')) ||
      (x["class"] === 'boundary' && x.type === 'administrative')
    )
  )

module.exports = (state = initialState, action = {}) => {

  switch (action.type) {

    case T.TOGGLE_SEARCH_CATEGORY:
      const c = action.payload;
      if (c == null) {
        return state;
      }
      //TODO: beautify
      const cats = state.categories.indexOf(c) >= 0
        ? (() => {
          var ref1 = state.categories;
          var results = [];
          var x, i, len;
          for (i = 0, len = ref1.length; i < len; i++) {
            x = ref1[i];
            if (x !== c) {
              results.push(x);
            }
          }
          return results;
        })()
      : [c].concat([].slice.call(state.categories));

      return {
        ...state,
        categories: cats
      }

    case T.SET_SEARCH_TEXT:
      return {
        ...state,
        text: action.payload
      }

    case T.UPDATE_STATE_FROM_URL:
      const searchText = parseUrl(action.payload).params.search;

      return {
        ...state,
        text: searchText || ""
      }

    case T.SET_CITY_SEARCH_TEXT:
      return {
        ...state,
        city: action.payload
      }

    case T.SEARCH_RESULT:
      if (!action.error) {
        return {
          ...state,
          result: action.payload.visible,
          invisible: action.payload.invisible
        }
      }
      return state;
      break;

    case T.SEARCH_ADDRESS_RESULT:
      if (!action.error) {
        const d = action.payload || [];
        return {
          ...state,
          addresses: d,
          cities: unique(d.filter(isCity)),
          error: false
        }
      } else {
        return {
          ...state,
          addresses: [],
          cities: [],
          error: true
        }
      }
      break;

    case T.NEW_ENTRY_RESULT:
      if (!action.error) {
        var newResult = state.result;
        newResult.push({id: action.payload.id});
        return {
          ...state,
          result: newResult
        }
      }
      break;

    case T.SET_CURRENT_ENTRY:
      const p = action.payload;
      if (p != null) {
        return {
          ...state,
{'fraction_non_alphanumeric': 0.14453434872283039, 'fraction_numerical': 0.0015865460891638901, 'mean_word_length': 1.505564387917329, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4786778', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1929, 'n_words': 435}