<commit_before>import {
  argsForFetch,
  parseResponse
} from './utils/request'
import 'cross-fetch'
import {uuidv4} from './utils/helpers'
import {needsRefresh} from './window'
import {withoutBZParams} from './utils/url'
import {parseSJR} from './utils/request'
import {
  SAVE_RESPONSE,
  HANDLE_GRAFT,
  SET_IN_PAGE,
  DEL_IN_PAGE,
  EXTEND_IN_PAGE,
  SET_IN_JOINT,
  DEL_IN_JOINT,
  EXTEND_IN_JOINT,
  BEFORE_FETCH,
  BREEZY_ERROR,
  OVERRIDE_VISIT_SEQ,
} from './actions'

export function saveResponse ({pageKey, page}) {
  return {
    type: SAVE_RESPONSE,
    payload: {
      pageKey,
      page
    }
  }
}

export function handleGraft ({pageKey, page}) {
  return {
    type: HANDLE_GRAFT,
    payload: {
      pageKey,
      page
    }
  }
}

export function setInPage ({pageKey, keypath, value}) {
  return {
    type: SET_IN_PAGE,
    payload: {
      pageKey,
      keypath,
      value
    }
  }
}

export function delInPage ({pageKey, keypath}) {
  return {
    type: DEL_IN_PAGE,
    payload: {
      pageKey,
      keypath,
    }
  }
}

export function extendInPage ({pageKey, keypath, value}) {
  return {
    type: EXTEND_IN_PAGE,
    payload:{
      pageKey,
      keypath,
      value
    }
  }
}

export function setInJoint ({name, keypath, value}) {
  return {
    type: SET_IN_JOINT,
    payload: {
      name,
      keypath,
      value
    }
  }
}

export function delInJoint ({name, keypath}) {
  return {
    type: DEL_IN_JOINT,
    payload: {
      name,
      keypath
    }
  }
}

export function extendInJoint ({name, keypath, value}) {
  return {
    type: EXTEND_IN_JOINT,
    payload: {
      name,
      keypath,
      value
    }
  }
}

export function beforeFetch (payload) {
  return {
    type: BEFORE_FETCH,
    payload,
  }
}

export function handleError (err) {
  return {
    type: BREEZY_ERROR,
    payload: {
      message: err.message
    }
  }
}

export function saveAndProcessSJRPage (pageKey, pageSJR) {
  const page = parseSJR(pageSJR)
  return saveAndProcessPage(pageKey, page)
}

export function isGraft(page) {
  return page.action === 'graft'
}

export function saveAndProcessPage (pageKey, page) {
  return (dispatch) => {
    const {defers = []} = page

    if (isGraft(page)) {
      dispatch(handleGraft({pageKey, page}))
    } else {
      dispatch(saveResponse({pageKey, page}))
    }

    defers.forEach(function ({url}){
      dispatch(remote(url, {}, pageKey))
    })
  }
}

function handleFetchErr (err, fetchArgs, dispatch) {
  err.fetchArgs = fetchArgs
  err.url = fetchArgs[0]
  err.pageKey = withoutBZParams(fetchArgs[0])
  dispatch(handleError(err))
  throw err
}

export function wrappedFetch (fetchArgs) {
  return fetch(...fetchArgs)
    .then((response) => {
      const location = response.headers.get('x-breezy-location')
      const nextOpts = {...fetchArgs[1], body: undefined}
      if (location) {
        return wrappedFetch([location, {...nextOpts, method: 'GET'}])
      } else {
        return response
      }
    })
}

const persistAndMeta = (state, rsp, page, pageKey, dispatch) => {
  const prevAssets = state.breezy.assets
  const newAssets = page.assets

  pageKey = withoutBZParams(pageKey)

  const meta = {
    pageKey,
    page,
    screen: page.screen,
    rsp,
    needsRefresh: needsRefresh(prevAssets, newAssets)
  }

  dispatch(saveAndProcessPage(pageKey, page))
  return meta
}

export function remote (pathQuery, {method = 'GET', headers, body = ''} = {}, pageKey) {
  if (!pageKey) {
    throw new Error('pageKey is a required parameter')<commit_msg>extract meta builder<commit_after>import {
  argsForFetch,
  parseResponse
} from './utils/request'
import 'cross-fetch'
import {uuidv4} from './utils/helpers'
import {needsRefresh} from './window'
import {withoutBZParams} from './utils/url'
import {parseSJR} from './utils/request'
import {
  SAVE_RESPONSE,
  HANDLE_GRAFT,
  SET_IN_PAGE,
  DEL_IN_PAGE,
  EXTEND_IN_PAGE,
  SET_IN_JOINT,
  DEL_IN_JOINT,
  EXTEND_IN_JOINT,
  BEFORE_FETCH,
  BREEZY_ERROR,
  OVERRIDE_VISIT_SEQ,
} from './actions'

export function saveResponse ({pageKey, page}) {
  return {
    type: SAVE_RESPONSE,
    payload: {
      pageKey,
      page
    }
  }
}

export function handleGraft ({pageKey, page}) {
  return {
    type: HANDLE_GRAFT,
    payload: {
      pageKey,
      page
    }
  }
}

export function setInPage ({pageKey, keypath, value}) {
  return {
    type: SET_IN_PAGE,
    payload: {
      pageKey,
      keypath,
      value
    }
  }
}

export function delInPage ({pageKey, keypath}) {
  return {
    type: DEL_IN_PAGE,
    payload: {
      pageKey,
      keypath,
    }
  }
}

export function extendInPage ({pageKey, keypath, value}) {
  return {
    type: EXTEND_IN_PAGE,
    payload:{
      pageKey,
      keypath,
      value
    }
  }
}

export function setInJoint ({name, keypath, value}) {
  return {
    type: SET_IN_JOINT,
    payload: {
      name,
      keypath,
      value
    }
  }
}

export function delInJoint ({name, keypath}) {
  return {
    type: DEL_IN_JOINT,
    payload: {
      name,
      keypath
    }
  }
}

export function extendInJoint ({name, keypath, value}) {
  return {
    type: EXTEND_IN_JOINT,
    payload: {
      name,
      keypath,
      value
    }
  }
}

export function beforeFetch (payload) {
  return {
    type: BEFORE_FETCH,
    payload,
  }
}

export function handleError (err) {
  return {
    type: BREEZY_ERROR,
    payload: {
      message: err.message
    }
  }
}

export function saveAndProcessSJRPage (pageKey, pageSJR) {
  const page = parseSJR(pageSJR)
  return saveAndProcessPage(pageKey, page)
}

export function isGraft(page) {
  return page.action === 'graft'
}

export function saveAndProcessPage (pageKey, page) {
  return (dispatch) => {
    const {defers = []} = page

    if (isGraft(page)) {
      dispatch(handleGraft({pageKey, page}))
    } else {
      dispatch(saveResponse({pageKey, page}))
    }

    defers.forEach(function ({url}){
      dispatch(remote(url, {}, pageKey))
    })
  }
}

function handleFetchErr (err, fetchArgs, dispatch) {
  err.fetchArgs = fetchArgs
  err.url = fetchArgs[0]
  err.pageKey = withoutBZParams(fetchArgs[0])
  dispatch(handleError(err))
  throw err
}

export function wrappedFetch (fetchArgs) {
  return fetch(...fetchArgs)
    .then((response) => {
      const location = response.headers.get('x-breezy-location')
      const nextOpts = {...fetchArgs[1], body: undefined}
      if (location) {
        return wrappedFetch([location, {...nextOpts, method: 'GET'}])
      } else {
        return response
      }
    })
}

function buildMeta (pageKey, page, {assets}) {
  const prevAssets = assets
  const newAssets = page.assets
  pageKey = withoutBZParams(pageKey)

  return {
    pageKey,
    page,
    screen: page.screen,
    needsRefresh: needsRefresh(prevAssets, newAssets)
  }
}

const persistAndMeta = (state, rsp, page, pageKey, dispatch) => {
  pageKey = withoutBZParams(pageKey)
  const meta = {...buildMeta(pageKey, pageKey, state.breezy), rsp}

  dispatch(saveAndProcessPage(pageKey, page))
  return meta
}

export function remote (pathQuery, {method = 'GET', headers, body = ''} = {}, pageKey) {
  if (!pageKey) {
    throw new Error('pageKey is a required parameter')
{'fraction_non_alphanumeric': 0.12088974854932302, 'fraction_numerical': 0.0011052777010223818, 'mean_word_length': 2.906637884511603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1528080', 'n_tokens_mistral': 2588, 'n_tokens_neox': 2514, 'n_words': 562}