<commit_before>/* @flow strict-local */
import {
  REALM_ADD,
  LOGIN_SUCCESS,
  ACCOUNT_SWITCH,
  LOGOUT,
  ACCOUNT_REMOVE,
} from '../actionConstants';

import type {
  AccountsState,
  AccountAction,
  RealmAddAction,
  AccountSwitchAction,
  AccountRemoveAction,
  LoginSuccessAction,
  LogoutAction,
} from '../types';
import { NULL_ARRAY } from '../nullObjects';

const initialState = NULL_ARRAY;

const realmAdd = (state: AccountsState, action: RealmAddAction): AccountsState => {
  const accountIndex = state.findIndex(account => account.realm === action.realm);

  if (accountIndex !== -1) {
    return [state[accountIndex], ...state.slice(0, accountIndex), ...state.slice(accountIndex + 1)];
  }

  return [
    {
      realm: action.realm,
      apiKey: '',
      email: '',
    },
    ...state,
  ];
};

const accountSwitch = (state: AccountsState, action: AccountSwitchAction): AccountsState => {
  if (action.index === 0) {
    return state;
  }

  return [state[action.index], ...state.slice(0, action.index), ...state.slice(action.index + 1)];
};

const loginSuccess = (state: AccountsState, action: LoginSuccessAction): AccountsState => {
  const { realm, email, apiKey } = action;
  const accountIndex = state.findIndex(
    account => account.realm === realm && (!account.email || account.email === email),
  );
  if (accountIndex === -1) {
    return [{ realm, email, apiKey }, ...state];
  }
  return [
    { ...state[accountIndex], email, apiKey },
    ...state.slice(0, accountIndex),
    ...state.slice(accountIndex + 1),
  ];
};

const logout = (state: AccountsState, action: LogoutAction): AccountsState => [
  { ...state[0], apiKey: '' },
  ...state.slice(1),
];

const accountRemove = (state: AccountsState, action: AccountRemoveAction): AccountsState => {
  const newState = state.slice();
  newState.splice(action.index, 1);
  return newState;
};

export default (state: AccountsState = initialState, action: AccountAction): AccountsState => {
  switch (action.type) {<commit_msg>account: Factor out small helper findAccount in reducer.<commit_after>/* @flow strict-local */
import {
  REALM_ADD,
  LOGIN_SUCCESS,
  ACCOUNT_SWITCH,
  LOGOUT,
  ACCOUNT_REMOVE,
} from '../actionConstants';

import type {
  AccountsState,
  Identity,
  AccountAction,
  RealmAddAction,
  AccountSwitchAction,
  AccountRemoveAction,
  LoginSuccessAction,
  LogoutAction,
} from '../types';
import { NULL_ARRAY } from '../nullObjects';

const initialState = NULL_ARRAY;

const realmAdd = (state: AccountsState, action: RealmAddAction): AccountsState => {
  const accountIndex = state.findIndex(account => account.realm === action.realm);

  if (accountIndex !== -1) {
    return [state[accountIndex], ...state.slice(0, accountIndex), ...state.slice(accountIndex + 1)];
  }

  return [
    {
      realm: action.realm,
      apiKey: '',
      email: '',
    },
    ...state,
  ];
};

const accountSwitch = (state: AccountsState, action: AccountSwitchAction): AccountsState => {
  if (action.index === 0) {
    return state;
  }

  return [state[action.index], ...state.slice(0, action.index), ...state.slice(action.index + 1)];
};

const findAccount = (state: AccountsState, identity: Identity): number => {
  const { realm, email } = identity;
  return state.findIndex(
    account => account.realm === realm && (!account.email || account.email === email),
  );
};

const loginSuccess = (state: AccountsState, action: LoginSuccessAction): AccountsState => {
  const { realm, email, apiKey } = action;
  const accountIndex = findAccount(state, { realm, email });
  if (accountIndex === -1) {
    return [{ realm, email, apiKey }, ...state];
  }
  return [
    { ...state[accountIndex], email, apiKey },
    ...state.slice(0, accountIndex),
    ...state.slice(accountIndex + 1),
  ];
};

const logout = (state: AccountsState, action: LogoutAction): AccountsState => [
  { ...state[0], apiKey: '' },
  ...state.slice(1),
];

const accountRemove = (state: AccountsState, action: AccountRemoveAction): AccountsState => {
  const newState = state.slice();
  newState.splice(action.index, 1);
  return newState;
};

export default (state: AccountsState = initialState, action: AccountAction): AccountsState => {
  switch (action.type) {
{'fraction_non_alphanumeric': 0.15970747817881575, 'fraction_numerical': 0.005661712668082095, 'mean_word_length': 3.790960451977401, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '2113100', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1322, 'n_words': 313}