{"size": 3006, "ext": "js", "max_stars_repo_path": "src/state/BattleManager.js", "max_stars_repo_name": "Paul-Ladyman/dnd-battle-tracker", "max_stars_repo_stars_event_min_datetime": "2019-02-07T14:18:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-15T14:51:25.000Z", "max_issues_repo_path": "src/state/BattleManager.js", "max_issues_repo_name": "Paul-Ladyman/dnd-battle-tracker", "max_issues_repo_issues_event_min_datetime": "2020-12-17T14:34:33.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T01:06:04.000Z", "max_forks_repo_path": "src/state/BattleManager.js", "max_forks_repo_name": "Paul-Ladyman/dnd-battle-tracker", "max_forks_repo_forks_event_min_datetime": "2020-02-27T16:54:29.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-11T02:34:14.000Z"}
{"max_stars_count": 14, "max_issues_count": 129, "max_forks_count": 3, "avg_line_length": 28.9038461538, "max_line_length": 98, "alphanum_fraction": 0.7238855622}
import { resetCreature } from './CreatureManager';
import { version } from '../../package.json';

function findCreatureIndex(creatures, creature) {
  return creatures.findIndex(({ id }) => creature.id === id);
}

export const newBattleState = {
  creatures: [],
  creatureIdCount: 0,
  activeCreature: null,
  focusedCreature: undefined,
  round: 0,
  ariaAnnouncements: [],
  errors: [],
  createCreatureErrors: {},
  battleId: undefined,
  battleCreated: false,
  shareEnabled: false,
  battleTrackerVersion: version,
  rulesSearchOpened: false,
};

export function nextFocus(state) {
  const { creatures, focusedCreature: currentFocusedCreature } = state;
  const creatureCount = creatures.length;
  if (creatureCount === 0) {
    return state;
  }

  let focusedCreature = 0;

  if (currentFocusedCreature !== undefined) {
    focusedCreature = currentFocusedCreature + 1;
  }

  if (focusedCreature === creatureCount) {
    focusedCreature = 0;
  }

  return { ...state, focusedCreature };
}

export function prevFocus(state) {
  const { creatures, focusedCreature: currentFocusedCreature } = state;
  const creatureCount = creatures.length;
  if (creatureCount === 0) {
    return state;
  }

  let focusedCreature = currentFocusedCreature - 1;

  if (currentFocusedCreature === undefined || currentFocusedCreature === 0) {
    focusedCreature = creatureCount - 1;
  }

  return { ...state, focusedCreature };
}

export function setFocus(state, creature) {
  let focusedCreature = findCreatureIndex(state.creatures, creature);
  if (focusedCreature === -1) {
    focusedCreature = 0;
  }
  return { ...state, focusedCreature };
}

export function resetBattle(state) {
  const {
    creatures,
    ariaAnnouncements: currentAriaAnnouncements,
    battleId,
    shareEnabled,
    battleCreated,
  } = state;
  const lockedCreatures = creatures.filter((creature) => creature.locked);
  const creatureIdCount = lockedCreatures.length;
  const resetLockedCreatures = lockedCreatures.map((creature, id) => resetCreature(id, creature));
  const ariaAnnouncements = currentAriaAnnouncements.concat(['battle reset']);
  return {
    ...newBattleState,
    battleCreated,
    shareEnabled,
    battleId,
    creatureIdCount,
    creatures: resetLockedCreatures,
    ariaAnnouncements,
  };
}

export function toggleSync(state) {
  const { shareEnabled, ariaAnnouncements: currentAriaAnnouncements } = state;
  const announcement = shareEnabled ? 'share disabled' : 'share enabled';
  const ariaAnnouncements = currentAriaAnnouncements.concat([announcement]);
  return { ...state, shareEnabled: !shareEnabled, ariaAnnouncements };
}

export function toggleRulesSearch(state) {
  const { rulesSearchOpened, ariaAnnouncements: currentAriaAnnouncements } = state;
  const announcement = rulesSearchOpened ? 'rules search closed' : 'rules search opened';
  const ariaAnnouncements = currentAriaAnnouncements.concat([announcement]);
  return { ...state, rulesSearchOpened: !rulesSearchOpened, ariaAnnouncements };
}
