<commit_before>import eeprom from '../utils/eeprom';

import {
  ALARM, ALARM_ENABLED, ALARM_VALUE, AS_BASE_STATE, BAUD_RATE, CHANNEL,
  FC_TYPE, FONT_SIZE, H_ALIGNMENT, MAX, MAX_PANELS, MIN, OFFSET, PANEL,
  PARAMS_FROM_EEPROM, POSITION, RADIUS, RAW, SCALE, SCALE_ALIGNMENT,
  SCALE_ENABLED, SCALE_TYPE, TYPE, UNITS, V_ALIGNMENT, VALUE, MODE,
  VISIBLE_ON,
} from './actions';

function addPreviewState(state) {
  return state
    .updateIn(['preview', 'panel'], () => 0)
    .updateIn(['preview', 'alarm'], () => 0);
}

function setAsBaseState(state, baseState = null) {
  return state.updateIn(['originalState'], () => baseState || state);
}

const initialState = setAsBaseState(addPreviewState(eeprom.toParameters(eeprom.defaultEEPROM)));

export default function parameters(state = initialState, action) {
  const parameterName = action.parameter;
  const payload = action.payload;

  switch (action.type) {
    case ALARM:
      return state.setIn([parameterName, 'alarm'], payload);
    case ALARM_ENABLED:
      return state.setIn([parameterName, `${payload.alarm}Enabled`], payload.enabled);
    case ALARM_VALUE:
      return state.setIn([parameterName, `${payload.alarm}Value`], payload.value);
    case AS_BASE_STATE:
      return setAsBaseState(state, action.state);
    case BAUD_RATE:
      return state.setIn([parameterName, 'baudRate'], payload);
    case CHANNEL:
      return state.setIn([parameterName, `${payload.key}Channel`], payload.channel);
    case FC_TYPE:
      return state.updateIn([parameterName, 'fcType'], () => action.fcType);
    case FONT_SIZE:
      return state.setIn([parameterName, 'fontSize'], payload);
    case H_ALIGNMENT:
      return state.setIn([parameterName, 'hAlignment'], payload);
    case MAX:
      return state.setIn([parameterName, 'max'], payload);
    case MAX_PANELS:
      return state.setIn([parameterName, 'maxPanels'], payload);
    case MIN:
      return state.setIn([parameterName, 'min'], payload);
    case OFFSET:
      return state
        .setIn([parameterName, 'offsetX'], payload.x)
        .setIn([parameterName, 'offsetY'], payload.y);
    case PANEL:
      return state.updateIn([parameterName, 'panel'], () => action.panel);
    case PARAMS_FROM_EEPROM: {
      let eepromData = action.eepromData;
      const defaultEEPROM = eeprom.defaultEEPROM;
      if (eepromData.size < defaultEEPROM.size) {
        const missingData = defaultEEPROM.slice(eepromData.size, defaultEEPROM.size);
        eepromData = eepromData.concat(missingData);
      }<commit_msg>fix(eeprom): fix adding missing values from default eeprom when loading<commit_after>import eeprom from '../utils/eeprom';

import {
  ALARM, ALARM_ENABLED, ALARM_VALUE, AS_BASE_STATE, BAUD_RATE, CHANNEL,
  FC_TYPE, FONT_SIZE, H_ALIGNMENT, MAX, MAX_PANELS, MIN, OFFSET, PANEL,
  PARAMS_FROM_EEPROM, POSITION, RADIUS, RAW, SCALE, SCALE_ALIGNMENT,
  SCALE_ENABLED, SCALE_TYPE, TYPE, UNITS, V_ALIGNMENT, VALUE, MODE,
  VISIBLE_ON,
} from './actions';

function addPreviewState(state) {
  return state
    .updateIn(['preview', 'panel'], () => 0)
    .updateIn(['preview', 'alarm'], () => 0);
}

function setAsBaseState(state, baseState = null) {
  return state.updateIn(['originalState'], () => baseState || state);
}

const initialState = setAsBaseState(addPreviewState(eeprom.toParameters(eeprom.defaultEEPROM)));

export default function parameters(state = initialState, action) {
  const parameterName = action.parameter;
  const payload = action.payload;

  switch (action.type) {
    case ALARM:
      return state.setIn([parameterName, 'alarm'], payload);
    case ALARM_ENABLED:
      return state.setIn([parameterName, `${payload.alarm}Enabled`], payload.enabled);
    case ALARM_VALUE:
      return state.setIn([parameterName, `${payload.alarm}Value`], payload.value);
    case AS_BASE_STATE:
      return setAsBaseState(state, action.state);
    case BAUD_RATE:
      return state.setIn([parameterName, 'baudRate'], payload);
    case CHANNEL:
      return state.setIn([parameterName, `${payload.key}Channel`], payload.channel);
    case FC_TYPE:
      return state.updateIn([parameterName, 'fcType'], () => action.fcType);
    case FONT_SIZE:
      return state.setIn([parameterName, 'fontSize'], payload);
    case H_ALIGNMENT:
      return state.setIn([parameterName, 'hAlignment'], payload);
    case MAX:
      return state.setIn([parameterName, 'max'], payload);
    case MAX_PANELS:
      return state.setIn([parameterName, 'maxPanels'], payload);
    case MIN:
      return state.setIn([parameterName, 'min'], payload);
    case OFFSET:
      return state
        .setIn([parameterName, 'offsetX'], payload.x)
        .setIn([parameterName, 'offsetY'], payload.y);
    case PANEL:
      return state.updateIn([parameterName, 'panel'], () => action.panel);
    case PARAMS_FROM_EEPROM: {
      let eepromData = action.eepromData;
      for (let i = eepromData.length; i > 0; i--) {
        if (eepromData[i - 1] !== 0) {
          eepromData = eepromData.slice(0, i);
          break;
        }
      }

      const defaultEEPROM = eeprom.defaultEEPROM;
      if (eepromData.length < defaultEEPROM.length) {
        const missingData = defaultEEPROM.slice(eepromData.length, defaultEEPROM.length);
        eepromData = eepromData.concat(missingData);
      }
{'fraction_non_alphanumeric': 0.13362231436110064, 'fraction_numerical': 0.0015077271013946476, 'mean_word_length': 4.006603773584906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, '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': '1317637', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1708, 'n_words': 372}