<commit_before>import { denormalize } from 'normalizr';
import includes from 'lodash/includes';
import mapValues from 'lodash/mapValues';
import { createSelector } from 'reselect';
import tacticDetailSchema from './schema';

const getSelectedTacticId = state => state.app.selectedTacticId;
const getFetching = state => state.entities.tacticDetails.status.fetching;
const getErrors = state => state.entities.tacticDetails.status.errors;
const getTeams = state => state.entities.teams;
const getPlayers = state => state.entities.players;
const getTacticDetails = state => state.entities.tacticDetails;

const getPlayersByPosForTeam = denormalizedTeam =>
  denormalizedTeam.players.reduce((result, player) =>
    ({ ...result, [player.position]: player }), {});

const getTeamsWithPlayersByPos = denormalizedTeams =>
  denormalizedTeams.map(team =>
    ({ ...team, players: getPlayersByPosForTeam(team) }),
  );

export const tacticDetailSelector = createSelector(
  [getTacticDetails, getTeams, getPlayers, getSelectedTacticId],
  (tacticDetails, teams, players, selectedTacticId) => {
    const entities = { tacticDetails, teams, players };
    const denormalized = denormalize(
      selectedTacticId,
      tacticDetailSchema,
      { ...mapValues(entities, value => value.byId) },
    );

    return denormalized ? {
      ...denormalized,
      teams: getTeamsWithPlayersByPos(denormalized.teams),
    } : null;
  },
);

export const isFetchingSelector = createSelector(
  [getFetching, getSelectedTacticId],
  (fetching, selectedTacticId) => includes(fetching, selectedTacticId),
);

export const hasErrorSelector = createSelector(
  [getErrors, getSelectedTacticId],
  (errors, selectedTacticId) => includes(errors, selectedTacticId),
);<commit_msg>Combine player transitions with player data in tacticDetails selector<commit_after>import { denormalize } from 'normalizr';
import includes from 'lodash/includes';
import mapValues from 'lodash/mapValues';
import { createSelector } from 'reselect';
import tacticDetailSchema from './schema';

const getSelectedTacticId = state => state.app.selectedTacticId;
const getFetching = state => state.entities.tacticDetails.status.fetching;
const getErrors = state => state.entities.tacticDetails.status.errors;
const getTeams = state => state.entities.teams;
const getPlayers = state => state.entities.players;
const getTacticDetails = state => state.entities.tacticDetails;
const getPlayerTransitions = state => state.editor.playerTransitions;

const getTeamWithPlayerTrans = (denormalizedTeam, playerTransitions) => {
  const playersWithTrans = denormalizedTeam.players.map(player =>
    ({ ...player, transition: playerTransitions[player.id] }),
  );
  return { ...denormalizedTeam, players: playersWithTrans };
};

const getPlayersByPosForTeam = denormalizedTeam =>
  denormalizedTeam.players.reduce((result, player) =>
    ({ ...result, [player.position]: player }), {});

const getTeamsWithPlayersByPos = denormalizedTeams =>
  denormalizedTeams.map(team =>
    ({ ...team, players: getPlayersByPosForTeam(team) }),
  );

export const tacticDetailSelector = createSelector(
  [getTacticDetails, getTeams, getPlayers, getSelectedTacticId, getPlayerTransitions],
  (tacticDetails, teams, players, selectedTacticId, playerTransitions) => {
    const entities = { tacticDetails, teams, players };
    const denormalized = denormalize(
      selectedTacticId,
      tacticDetailSchema,
      { ...mapValues(entities, value => value.byId) },
    );

    if (denormalized) {
      const teamsWithPlayerTrans = denormalized.teams.map(team =>
        getTeamWithPlayerTrans(team, playerTransitions),
      );
      return {
        ...denormalized,
        teams: getTeamsWithPlayersByPos(teamsWithPlayerTrans),
      };
    }
    return null;
  },
);

export const isFetchingSelector = createSelector(
  [getFetching, getSelectedTacticId],
  (fetching, selectedTacticId) => includes(fetching, selectedTacticId),
);

export const hasErrorSelector = createSelector(
  [getErrors, getSelectedTacticId],
  (errors, selectedTacticId) => includes(errors, selectedTacticId),
);
{'fraction_non_alphanumeric': 0.11518833535844472, 'fraction_numerical': 0.0, 'mean_word_length': 5.044052863436123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '6257400', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1208, 'n_words': 255}