<commit_before>import createSiteSocketAction from '../action-creators/site-socket-action-creator'
import fetch from '../network/fetch'
import {
  MATCHMAKING_ACCEPT_BEGIN,
  MATCHMAKING_ACCEPT,
  MATCHMAKING_CANCEL_BEGIN,
  MATCHMAKING_CANCEL,
  MATCHMAKING_FIND_BEGIN,
  MATCHMAKING_FIND,
  MATCHMAKING_GET_CURRENT_MAP_POOL_BEGIN,
  MATCHMAKING_GET_CURRENT_MAP_POOL,
} from '../actions'
import { MatchmakingType } from '../../common/matchmaking'
import { ThunkAction } from '../dispatch-registry'
import { RaceChar } from '../../common/races'
import { GetPreferencesPayload, MatchmakingPreferences } from './actions'

export const findMatch = (
  type: MatchmakingType,
  race: RaceChar,
  useAlternateRace: boolean,
  alternateRace: RaceChar,
  preferredMaps: string[],
) =>
  createSiteSocketAction(MATCHMAKING_FIND_BEGIN, MATCHMAKING_FIND, '/matchmaking/find', {
    type,
    race,
    useAlternateRace,
    alternateRace,
    preferredMaps,
  })

export const cancelFindMatch = () =>
  createSiteSocketAction(MATCHMAKING_CANCEL_BEGIN, MATCHMAKING_CANCEL, '/matchmaking/cancel')

export const acceptMatch = () =>
  createSiteSocketAction(MATCHMAKING_ACCEPT_BEGIN, MATCHMAKING_ACCEPT, '/matchmaking/accept')

// TODO(2Pac): This can be cached
export function getCurrentMapPool(type: MatchmakingType): ThunkAction {
  return dispatch => {
    dispatch({
      type: MATCHMAKING_GET_CURRENT_MAP_POOL_BEGIN,
      meta: { type },
    } as any)
    dispatch({
      type: MATCHMAKING_GET_CURRENT_MAP_POOL,
      payload: fetch('/api/1/matchmakingMapPools/' + encodeURIComponent(type) + '/current'),
      meta: { type },
    } as any)
  }
}

export function getMatchmakingPreferences(matchmakingType: MatchmakingType): ThunkAction {
  return dispatch => {
    dispatch({ type: '@matchmaking/getPreferencesBegin', payload: { type: matchmakingType } })
    dispatch({
      type: '@matchmaking/getPreferences',
      payload: fetch<GetPreferencesPayload>(
        `/api/1/matchmakingPreferences?matchmakingType=${matchmakingType}`,
      ),
      meta: { type: matchmakingType },
    })
  }
}

// TODO(tec27): type the preferences object properly
export function updateMatchmakingPreferences(preferences: MatchmakingPreferences): ThunkAction {
  return dispatch => {<commit_msg>Remove a TODO that was actually already done.<commit_after>import createSiteSocketAction from '../action-creators/site-socket-action-creator'
import fetch from '../network/fetch'
import {
  MATCHMAKING_ACCEPT_BEGIN,
  MATCHMAKING_ACCEPT,
  MATCHMAKING_CANCEL_BEGIN,
  MATCHMAKING_CANCEL,
  MATCHMAKING_FIND_BEGIN,
  MATCHMAKING_FIND,
  MATCHMAKING_GET_CURRENT_MAP_POOL_BEGIN,
  MATCHMAKING_GET_CURRENT_MAP_POOL,
} from '../actions'
import { MatchmakingType } from '../../common/matchmaking'
import { ThunkAction } from '../dispatch-registry'
import { RaceChar } from '../../common/races'
import { GetPreferencesPayload, MatchmakingPreferences } from './actions'

export const findMatch = (
  type: MatchmakingType,
  race: RaceChar,
  useAlternateRace: boolean,
  alternateRace: RaceChar,
  preferredMaps: string[],
) =>
  createSiteSocketAction(MATCHMAKING_FIND_BEGIN, MATCHMAKING_FIND, '/matchmaking/find', {
    type,
    race,
    useAlternateRace,
    alternateRace,
    preferredMaps,
  })

export const cancelFindMatch = () =>
  createSiteSocketAction(MATCHMAKING_CANCEL_BEGIN, MATCHMAKING_CANCEL, '/matchmaking/cancel')

export const acceptMatch = () =>
  createSiteSocketAction(MATCHMAKING_ACCEPT_BEGIN, MATCHMAKING_ACCEPT, '/matchmaking/accept')

// TODO(2Pac): This can be cached
export function getCurrentMapPool(type: MatchmakingType): ThunkAction {
  return dispatch => {
    dispatch({
      type: MATCHMAKING_GET_CURRENT_MAP_POOL_BEGIN,
      meta: { type },
    } as any)
    dispatch({
      type: MATCHMAKING_GET_CURRENT_MAP_POOL,
      payload: fetch('/api/1/matchmakingMapPools/' + encodeURIComponent(type) + '/current'),
      meta: { type },
    } as any)
  }
}

export function getMatchmakingPreferences(matchmakingType: MatchmakingType): ThunkAction {
  return dispatch => {
    dispatch({ type: '@matchmaking/getPreferencesBegin', payload: { type: matchmakingType } })
    dispatch({
      type: '@matchmaking/getPreferences',
      payload: fetch<GetPreferencesPayload>(
        `/api/1/matchmakingPreferences?matchmakingType=${matchmakingType}`,
      ),
      meta: { type: matchmakingType },
    })
  }
}

export function updateMatchmakingPreferences(preferences: MatchmakingPreferences): ThunkAction {
  return dispatch => {
{'fraction_non_alphanumeric': 0.10990951224895167, 'fraction_numerical': 0.0017656146546016333, 'mean_word_length': 4.665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '1196843', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1391, 'n_words': 269}