<commit_before>import { API_URL } from '../app/config'
import { showStatusMessage } from '../app/status_message'
import { t } from '../app/locale'
import { app } from '../preinit/app_settings'
import {
  getAuthHeader,
  getSignInData,
  isSignedIn
} from '../users/authentication'
import { newBlockingAjaxRequest } from '../util/fetch_blocking'
import { setStreetCreatorId } from './data_model'
import { updateStreetName } from './name'
import { getUndoStack, getUndoPosition, unifyUndoStack } from './undo_stack'
import { saveStreetToServer, packServerStreetData, setStreetId } from './xhr'
import store from '../store'
import {
  saveStreetName,
  updateEditCount,
  saveOriginalStreetId
} from '../store/actions/street'

const STREET_NAME_REMIX_SUFFIX = '(remix)'
let remixOnFirstEdit = false

export function getRemixOnFirstEdit () {
  return remixOnFirstEdit
}

export function setRemixOnFirstEdit (value) {
  remixOnFirstEdit = value
}

// Auto “promote” (remix) the street if you just signed in and the street
// was anonymous
let promoteStreet = false

export function getPromoteStreet () {
  return promoteStreet
}

export function setPromoteStreet (value) {
  promoteStreet = value
}

export function remixStreet () {
  let dontAddSuffix
  if (app.readOnly) {
    return
  }

  remixOnFirstEdit = false

  if (isSignedIn()) {
    setStreetCreatorId(getSignInData().userId)
  } else {
    setStreetCreatorId(null)
  }

  const street = store.getState().street
  store.dispatch(saveOriginalStreetId(street.id))
  store.dispatch(updateEditCount(0))

  unifyUndoStack()

  var undoStack = getUndoStack()
  var undoPosition = getUndoPosition()
  if (undoStack[undoPosition - 1] && (undoStack[undoPosition - 1].name !== street.name)) {
    // The street was remixed as a result of editing its name. Don’t be
    // a douche and add (remixed) to it then.
    dontAddSuffix = true
  } else {
    dontAddSuffix = false
  }

  if (!promoteStreet && !dontAddSuffix) {
    addRemixSuffixToName()
  }

  var transmission = packServerStreetData()

  newBlockingAjaxRequest(t('msg.remixing'),
    {
      // TODO const
      url: API_URL + 'v1/streets',
      method: 'POST',
      body: transmission,
      headers: {
        'Authorization': getAuthHeader(),
        'Content-Type': 'application/json'
      }
    }, receiveRemixedStreet
  )
}

function receiveRemixedStreet (data) {
  if (!promoteStreet) {
    if (isSignedIn()) {
      showStatusMessage(t('toast.remixing'))
    } else {
      showStatusMessage(t('toast.remixing-sign-in'), false, true)
    }
  }

  setStreetId(data.id, data.namespacedId)
  updateStreetName(store.getState().street)

  saveStreetToServer(false)
}

export function addRemixSuffixToName () {
  const street = store.getState().street
  if (street.name.substr(street.name.length - STREET_NAME_REMIX_SUFFIX.length,
    STREET_NAME_REMIX_SUFFIX.length) !== STREET_NAME_REMIX_SUFFIX) {
    street.name += ' ' + STREET_NAME_REMIX_SUFFIX<commit_msg>avoid appending (remix) when street name is null<commit_after>import { API_URL } from '../app/config'
import { showStatusMessage } from '../app/status_message'
import { t } from '../app/locale'
import { app } from '../preinit/app_settings'
import {
  getAuthHeader,
  getSignInData,
  isSignedIn
} from '../users/authentication'
import { newBlockingAjaxRequest } from '../util/fetch_blocking'
import { setStreetCreatorId } from './data_model'
import { updateStreetName } from './name'
import { getUndoStack, getUndoPosition, unifyUndoStack } from './undo_stack'
import { saveStreetToServer, packServerStreetData, setStreetId } from './xhr'
import store from '../store'
import {
  saveStreetName,
  updateEditCount,
  saveOriginalStreetId
} from '../store/actions/street'

const STREET_NAME_REMIX_SUFFIX = '(remix)'
let remixOnFirstEdit = false

export function getRemixOnFirstEdit () {
  return remixOnFirstEdit
}

export function setRemixOnFirstEdit (value) {
  remixOnFirstEdit = value
}

// Auto “promote” (remix) the street if you just signed in and the street
// was anonymous
let promoteStreet = false

export function getPromoteStreet () {
  return promoteStreet
}

export function setPromoteStreet (value) {
  promoteStreet = value
}

export function remixStreet () {
  let dontAddSuffix
  if (app.readOnly) {
    return
  }

  remixOnFirstEdit = false

  if (isSignedIn()) {
    setStreetCreatorId(getSignInData().userId)
  } else {
    setStreetCreatorId(null)
  }

  const street = store.getState().street
  store.dispatch(saveOriginalStreetId(street.id))
  store.dispatch(updateEditCount(0))

  unifyUndoStack()

  var undoStack = getUndoStack()
  var undoPosition = getUndoPosition()
  if (undoStack[undoPosition - 1] && (undoStack[undoPosition - 1].name !== street.name)) {
    // The street was remixed as a result of editing its name. Don’t be
    // a douche and add (remixed) to it then.
    dontAddSuffix = true
  } else {
    dontAddSuffix = false
  }

  if (!promoteStreet && !dontAddSuffix) {
    addRemixSuffixToName()
  }

  var transmission = packServerStreetData()

  newBlockingAjaxRequest(t('msg.remixing'),
    {
      // TODO const
      url: API_URL + 'v1/streets',
      method: 'POST',
      body: transmission,
      headers: {
        'Authorization': getAuthHeader(),
        'Content-Type': 'application/json'
      }
    }, receiveRemixedStreet
  )
}

function receiveRemixedStreet (data) {
  if (!promoteStreet) {
    if (isSignedIn()) {
      showStatusMessage(t('toast.remixing'))
    } else {
      showStatusMessage(t('toast.remixing-sign-in'), false, true)
    }
  }

  setStreetId(data.id, data.namespacedId)
  updateStreetName(store.getState().street)

  saveStreetToServer(false)
}

export function addRemixSuffixToName () {
  const street = store.getState().street

  // Bail if street is unnamed
  if (!street.name) return

  if (street.name.substr(street.name.length - STREET_NAME_REMIX_SUFFIX.length,
    STREET_NAME_REMIX_SUFFIX.length) !== STREET_NAME_REMIX_SUFFIX) {
    street.name += ' ' + STREET_NAME_REMIX_SUFFIX
{'fraction_non_alphanumeric': 0.11480562448304384, 'fraction_numerical': 0.0013234077750206782, 'mean_word_length': 3.860128617363344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4602391', 'n_tokens_mistral': 2121, 'n_tokens_neox': 1973, 'n_words': 440}