<commit_before>// @flow
import isPromise from 'is-promise'
import type { SubmitFunction } from './types'
import type { Props } from './createReduxForm'
import SubmissionError from './SubmissionError'
import { List } from 'immutable'

const isSubmissionError = error => error && error.name === SubmissionError.name

const mergeErrors = ({ asyncErrors, syncErrors }) =>
  asyncErrors && typeof asyncErrors.merge === 'function'
    ? asyncErrors.merge(syncErrors).toJS()
    : { ...asyncErrors, ...syncErrors }

// fields may be an Immutable List which cannot be spread
// convert the fields to an array if necessary
const makeFieldsArray = (fields: string[] | List<string>) =>
  List.isList(fields) ? ((fields: any): List<string>).toArray() : fields

const executeSubmit = (
  submit: SubmitFunction,
  fields: string[] | List<string>,
  props: Props
) => {
  const {
    dispatch,
    submitAsSideEffect,
    onSubmitFail,
    onSubmitSuccess,
    startSubmit,
    stopSubmit,
    setSubmitFailed,
    setSubmitSucceeded,
    values
  } = props

  fields = makeFieldsArray(fields)

  let result
  try {
    result = submit(values, dispatch, props)
  } catch (submitError) {
    const error = isSubmissionError(submitError)
      ? submitError.errors
      : undefined
    stopSubmit(error)
    setSubmitFailed(...fields)
    if (onSubmitFail) {
      onSubmitFail(error, dispatch, submitError, props)
    }
    if (error || onSubmitFail) {
      // if you've provided an onSubmitFail callback, don't re-throw the error
      return error
    } else {
      throw submitError
    }
  }
  if (submitAsSideEffect) {
    if (result) {
      dispatch(result)
    }
  } else {
    if (isPromise(result)) {
      startSubmit()
      return result.then(
        submitResult => {
          stopSubmit()
          setSubmitSucceeded()
          if (onSubmitSuccess) {
            onSubmitSuccess(submitResult, dispatch, props)
          }
          return submitResult
        },
        submitError => {
          const error = isSubmissionError(submitError)
            ? submitError.errors
            : undefined
          stopSubmit(error)
          setSubmitFailed(...fields)
          if (onSubmitFail) {
            onSubmitFail(error, dispatch, submitError, props)
          }
          if (error || onSubmitFail) {
            // if you've provided an onSubmitFail callback, don't re-throw the error
            return error
          } else {
            throw submitError
          }
        }
      )
    } else {
      setSubmitSucceeded()
      if (onSubmitSuccess) {
        onSubmitSuccess(result, dispatch, props)
      }
    }
  }

  return result
}

const handleSubmit = (
  submit: SubmitFunction,
  props: Props,
  valid: boolean,
  asyncValidate: Function,
  fields: string[] | List<string>
) => {
  const {
    dispatch,
    onSubmitFail,
    setSubmitFailed,
    syncErrors,
    asyncErrors,
    touch,
    persistentSubmitErrors
  } = props

  fields = makeFieldsArray(fields)

  touch(...fields) // mark all fields as touched

  if (valid || persistentSubmitErrors) {
    const asyncValidateResult = asyncValidate && asyncValidate()
    if (asyncValidateResult) {
      return asyncValidateResult
        .then(asyncErrors => {
          if (asyncErrors) {
            throw asyncErrors
          }
          return executeSubmit(submit, fields, props)
        })
        .catch(asyncErrors => {
          setSubmitFailed(...fields)
          if (onSubmitFail) {
            onSubmitFail(asyncErrors, dispatch, null, props)
          }
          return Promise.reject(asyncErrors)
        })
    } else {
      return executeSubmit(submit, fields, props)
    }
  } else {
    setSubmitFailed(...fields)
    const errors = mergeErrors({ asyncErrors, syncErrors })
    if (onSubmitFail) {
      onSubmitFail(errors, dispatch, null, props)
    }
    return errors
  }
}

export default handleSubmit
<commit_msg>Revert "Fix immutable support" (#4474)<commit_after>// @flow
import isPromise from 'is-promise'
import type { SubmitFunction } from './types'
import type { Props } from './createReduxForm'
import SubmissionError from './SubmissionError'

const isSubmissionError = error => error && error.name === SubmissionError.name

const mergeErrors = ({ asyncErrors, syncErrors }) =>
  asyncErrors && typeof asyncErrors.merge === 'function'
    ? asyncErrors.merge(syncErrors).toJS()
    : { ...asyncErrors, ...syncErrors }

const executeSubmit = (
  submit: SubmitFunction,
  fields: string[],
  props: Props
) => {
  const {
    dispatch,
    submitAsSideEffect,
    onSubmitFail,
    onSubmitSuccess,
    startSubmit,
    stopSubmit,
    setSubmitFailed,
    setSubmitSucceeded,
    values
  } = props

  let result
  try {
    result = submit(values, dispatch, props)
  } catch (submitError) {
    const error = isSubmissionError(submitError)
      ? submitError.errors
      : undefined
    stopSubmit(error)
    setSubmitFailed(...fields)
    if (onSubmitFail) {
      onSubmitFail(error, dispatch, submitError, props)
    }
    if (error || onSubmitFail) {
      // if you've provided an onSubmitFail callback, don't re-throw the error
      return error
    } else {
      throw submitError
    }
  }
  if (submitAsSideEffect) {
    if (result) {
      dispatch(result)
    }
  } else {
    if (isPromise(result)) {
      startSubmit()
      return result.then(
        submitResult => {
          stopSubmit()
          setSubmitSucceeded()
          if (onSubmitSuccess) {
            onSubmitSuccess(submitResult, dispatch, props)
          }
          return submitResult
        },
        submitError => {
          const error = isSubmissionError(submitError)
            ? submitError.errors
            : undefined
          stopSubmit(error)
          setSubmitFailed(...fields)
          if (onSubmitFail) {
            onSubmitFail(error, dispatch, submitError, props)
          }
          if (error || onSubmitFail) {
            // if you've provided an onSubmitFail callback, don't re-throw the error
            return error
          } else {
            throw submitError
          }
        }
      )
    } else {
      setSubmitSucceeded()
      if (onSubmitSuccess) {
        onSubmitSuccess(result, dispatch, props)
      }
    }
  }

  return result
}

const handleSubmit = (
  submit: SubmitFunction,
  props: Props,
  valid: boolean,
  asyncValidate: Function,
  fields: string[]
) => {
  const {
    dispatch,
    onSubmitFail,
    setSubmitFailed,
    syncErrors,
    asyncErrors,
    touch,
    persistentSubmitErrors
  } = props

  touch(...Array.from(fields)) // mark all fields as touched

  if (valid || persistentSubmitErrors) {
    const asyncValidateResult = asyncValidate && asyncValidate()
    if (asyncValidateResult) {
      return asyncValidateResult
        .then(asyncErrors => {
          if (asyncErrors) {
            throw asyncErrors
          }
          return executeSubmit(submit, fields, props)
        })
        .catch(asyncErrors => {
          setSubmitFailed(...fields)
          if (onSubmitFail) {
            onSubmitFail(asyncErrors, dispatch, null, props)
          }
          return Promise.reject(asyncErrors)
        })
    } else {
      return executeSubmit(submit, fields, props)
    }
  } else {
    setSubmitFailed(...fields)
    const errors = mergeErrors({ asyncErrors, syncErrors })
    if (onSubmitFail) {
      onSubmitFail(errors, dispatch, null, props)
    }
    return errors
  }
}

export default handleSubmit

{'fraction_non_alphanumeric': 0.09997337593184238, 'fraction_numerical': 0.0005324813631522897, 'mean_word_length': 2.318462897526502, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, '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': '766837', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2098, 'n_words': 519}