<commit_before>// This reducer should handle web entities status transitions, not implemented yet

import mergeWith from 'lodash.mergewith'
import set from 'lodash.set'
import uniq from 'lodash.uniq'
import without from 'lodash.without'
import createReducer from '../utils/create-reducer'
import {
  DECLARE_PAGE_SUCCESS,
  SET_WEBENTITY_HOMEPAGE_REQUEST,
  SET_WEBENTITY_HOMEPAGE_SUCCESS,
  SET_WEBENTITY_HOMEPAGE_FAILURE,
  SET_WEBENTITY_NAME_REQUEST,
  SET_WEBENTITY_NAME_SUCCESS,
  SET_WEBENTITY_NAME_FAILURE,
  SET_WEBENTITY_STATUS_REQUEST,
  SET_WEBENTITY_STATUS_SUCCESS,
  SET_WEBENTITY_STATUS_FAILURE,
  SET_TAB_WEBENTITY,
  CREATE_WEBENTITY_SUCCESS,
  ADJUST_WEBENTITY
  // Note we don't subscribe to SAVE_ADJUSTED_WEBENTITY_* because we're already plugged to its sub-actions
} from '../actions/webentities'
import { SELECT_CORPUS } from '../actions/corpora'
import {
  ADD_TAG_REQUEST,
  ADD_TAG_SUCCESS,
  ADD_TAG_FAILURE,
  REMOVE_TAG_REQUEST,
  REMOVE_TAG_SUCCESS,
  REMOVE_TAG_FAILURE
} from '../actions/tags'

const initialState = {
  webentities: {}, // id → WebEntity
  tabs: {}, // tabId → webEntityId
  adjustments: {} // webEntityId → adjustment { name, homepage, prefix, crawl }
}


export default createReducer(initialState, {

  [DECLARE_PAGE_SUCCESS]: (state, { webentity }) => ({
    ...state,
    webentities: {
      ...state.webentities,
      [webentity.id]: webentity
    }
  }),

  ...optimisticUpdateWebentity(
    'homepage',
    SET_WEBENTITY_HOMEPAGE_REQUEST,
    SET_WEBENTITY_HOMEPAGE_SUCCESS,
    SET_WEBENTITY_HOMEPAGE_FAILURE
  ),

  ...optimisticUpdateWebentity(
    'name',
    SET_WEBENTITY_NAME_REQUEST,
    SET_WEBENTITY_NAME_SUCCESS,
    SET_WEBENTITY_NAME_FAILURE
  ),

  ...optimisticUpdateWebentity(
    'status',
    SET_WEBENTITY_STATUS_REQUEST,
    SET_WEBENTITY_STATUS_SUCCESS,
    SET_WEBENTITY_STATUS_FAILURE
  ),

  // (Optimistically) add tag
  [ADD_TAG_REQUEST]: updateWebentity((webentity, { category, value, updatedValue }) => {
    const oldTags = webentity.tags.USER[category] || []
    const newTags = updatedValue
      ? oldTags.map((v) => (v === updatedValue) ? value : v)
      : uniq(oldTags.concat([value]))
    return set({ ['tags_' + category + '_prev']: oldTags }, 'tags.USER.' + category, newTags)
  }),
  [ADD_TAG_SUCCESS]: updateWebentity((webentity, { category }) => ({
    ['tags_' + category + '_prev']: null
  })),
  [ADD_TAG_FAILURE]: updateWebentity((webentity, { category }) => set(
    { ['tags_' + category + '_prev']: null },
    'tags.USER.' + category, webentity['tags_' + category + '_prev']
  )),

  // (Optimistically) remove tag
  [REMOVE_TAG_REQUEST]: updateWebentity((webentity, { category, value }) => set(<commit_msg>fix(tags): handle case where webentity.tags.USER is not defined<commit_after>// This reducer should handle web entities status transitions, not implemented yet

import mergeWith from 'lodash.mergewith'
import set from 'lodash.set'
import uniq from 'lodash.uniq'
import without from 'lodash.without'
import createReducer from '../utils/create-reducer'
import {
  DECLARE_PAGE_SUCCESS,
  SET_WEBENTITY_HOMEPAGE_REQUEST,
  SET_WEBENTITY_HOMEPAGE_SUCCESS,
  SET_WEBENTITY_HOMEPAGE_FAILURE,
  SET_WEBENTITY_NAME_REQUEST,
  SET_WEBENTITY_NAME_SUCCESS,
  SET_WEBENTITY_NAME_FAILURE,
  SET_WEBENTITY_STATUS_REQUEST,
  SET_WEBENTITY_STATUS_SUCCESS,
  SET_WEBENTITY_STATUS_FAILURE,
  SET_TAB_WEBENTITY,
  CREATE_WEBENTITY_SUCCESS,
  ADJUST_WEBENTITY
  // Note we don't subscribe to SAVE_ADJUSTED_WEBENTITY_* because we're already plugged to its sub-actions
} from '../actions/webentities'
import { SELECT_CORPUS } from '../actions/corpora'
import {
  ADD_TAG_REQUEST,
  ADD_TAG_SUCCESS,
  ADD_TAG_FAILURE,
  REMOVE_TAG_REQUEST,
  REMOVE_TAG_SUCCESS,
  REMOVE_TAG_FAILURE
} from '../actions/tags'

const initialState = {
  webentities: {}, // id → WebEntity
  tabs: {}, // tabId → webEntityId
  adjustments: {} // webEntityId → adjustment { name, homepage, prefix, crawl }
}


export default createReducer(initialState, {

  [DECLARE_PAGE_SUCCESS]: (state, { webentity }) => ({
    ...state,
    webentities: {
      ...state.webentities,
      [webentity.id]: webentity
    }
  }),

  ...optimisticUpdateWebentity(
    'homepage',
    SET_WEBENTITY_HOMEPAGE_REQUEST,
    SET_WEBENTITY_HOMEPAGE_SUCCESS,
    SET_WEBENTITY_HOMEPAGE_FAILURE
  ),

  ...optimisticUpdateWebentity(
    'name',
    SET_WEBENTITY_NAME_REQUEST,
    SET_WEBENTITY_NAME_SUCCESS,
    SET_WEBENTITY_NAME_FAILURE
  ),

  ...optimisticUpdateWebentity(
    'status',
    SET_WEBENTITY_STATUS_REQUEST,
    SET_WEBENTITY_STATUS_SUCCESS,
    SET_WEBENTITY_STATUS_FAILURE
  ),

  // (Optimistically) add tag
  [ADD_TAG_REQUEST]: updateWebentity((webentity, { category, value, updatedValue }) => {
    const oldTags = ((webentity.tags || {}).USER || {})[category] || []
    const newTags = updatedValue
      ? oldTags.map((v) => (v === updatedValue) ? value : v)
      : uniq(oldTags.concat([value]))
    return set({ ['tags_' + category + '_prev']: oldTags }, 'tags.USER.' + category, newTags)
  }),
  [ADD_TAG_SUCCESS]: updateWebentity((webentity, { category }) => ({
    ['tags_' + category + '_prev']: null
  })),
  [ADD_TAG_FAILURE]: updateWebentity((webentity, { category }) => set(
    { ['tags_' + category + '_prev']: null },
    'tags.USER.' + category, webentity['tags_' + category + '_prev']
  )),

  // (Optimistically) remove tag
  [REMOVE_TAG_REQUEST]: updateWebentity((webentity, { category, value }) => set(
{'fraction_non_alphanumeric': 0.12149362477231329, 'fraction_numerical': 0.0, 'mean_word_length': 4.37279843444227, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3799398', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1898, 'n_words': 358}