<commit_before>import {requestSaga} from 'tocco-util/src/rest'
import _get from 'lodash/get'

import {call} from 'redux-saga/effects'

const getCreatePermission = json => {
  if (json.createPermission && typeof json.createPermission === 'boolean') {
    return json.createPermission
  }

  return false
}

export const defaultModelTransformer = json => {
  const model = {}
  json.fields.forEach(field => {
    model[field.fieldName] = {
      ...field
    }
  })

  json.relations.forEach(relation => {
    model[relation.relationName] = {
      type: 'relation',
      targetEntity: relation.targetEntity,
      multi: relation.multi
    }
  })

  const createPermission = getCreatePermission(json)
  return {model, createPermission}
}

export function* fetchModel(entityName, transformer = defaultModelTransformer) {
  const resp = yield call(requestSaga, `entities/${entityName}/model`)
  return transformer(resp.body)
}

export const entitiesListTransformer = json => {
  return json.data.map(entity => {
    const result = {
      __key: entity.key,
      __model: entity.model
    }

    const paths = entity.paths
    for (const path in paths) {
      const type = paths[path].type
      if (type === 'field') {
        result[path] = paths[path].value
      } else if (type === 'entity') {
        result[path] = {
          type: 'string',
          value: paths[path].value ? paths[path].value.display : ''
        }
      } else if (type === 'entity-list') {
        result[path] = {
          type: 'string',
          value: paths[path].value ? paths[path].value.map(v => v.display).join(', ') : ''
        }
      } else if (type === 'display-expression') {
        result[path] = {
          type: 'html',
          value: paths[path].value
        }
      }
    }
    return result
  })
}

export const defaultEntitiesTransformer = json => (json)
export const selectEntitiesTransformer = json => (json.data.map(e => ({display: e.display, key: e.key})))
export const searchFilterTransformer = json =>
  (json.data.map(e => ({display: e.display, key: e.key, uniqueId: _get(e, 'paths.unique_id.value.value', null)})))

const getSortString = sorting => {
  if (Array.isArray(sorting) && sorting.length >= 1) {
    return sorting.map(sort => `${sort.field} ${sort.order}`).join(', ')<commit_msg>refactor(entity-list): add default for unhandled list types<commit_after>import {requestSaga} from 'tocco-util/src/rest'
import _get from 'lodash/get'

import {call} from 'redux-saga/effects'

const getCreatePermission = json => {
  if (json.createPermission && typeof json.createPermission === 'boolean') {
    return json.createPermission
  }

  return false
}

export const defaultModelTransformer = json => {
  const model = {}
  json.fields.forEach(field => {
    model[field.fieldName] = {
      ...field
    }
  })

  json.relations.forEach(relation => {
    model[relation.relationName] = {
      type: 'relation',
      targetEntity: relation.targetEntity,
      multi: relation.multi
    }
  })

  const createPermission = getCreatePermission(json)
  return {model, createPermission}
}

export function* fetchModel(entityName, transformer = defaultModelTransformer) {
  const resp = yield call(requestSaga, `entities/${entityName}/model`)
  return transformer(resp.body)
}

export const entitiesListTransformer = json => {
  return json.data.map(entity => {
    const result = {
      __key: entity.key,
      __model: entity.model
    }

    const paths = entity.paths
    for (const path in paths) {
      const type = paths[path].type
      if (type === 'field') {
        result[path] = paths[path].value
      } else if (type === 'entity') {
        result[path] = {
          type: 'string',
          value: paths[path].value ? paths[path].value.display : ''
        }
      } else if (type === 'entity-list') {
        result[path] = {
          type: 'string',
          value: paths[path].value ? paths[path].value.map(v => v.display).join(', ') : ''
        }
      } else if (type === 'display-expression') {
        result[path] = {
          type: 'html',
          value: paths[path].value
        }
      } else {
        result[path] = {
          type: 'string',
          value: ''
        }
      }
    }
    return result
  })
}

export const defaultEntitiesTransformer = json => (json)
export const selectEntitiesTransformer = json => (json.data.map(e => ({display: e.display, key: e.key})))
export const searchFilterTransformer = json =>
  (json.data.map(e => ({display: e.display, key: e.key, uniqueId: _get(e, 'paths.unique_id.value.value', null)})))

const getSortString = sorting => {
  if (Array.isArray(sorting) && sorting.length >= 1) {
    return sorting.map(sort => `${sort.field} ${sort.order}`).join(', ')
{'fraction_non_alphanumeric': 0.14928511354079058, 'fraction_numerical': 0.00042052144659377626, 'mean_word_length': 2.870626525630594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '1206375', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1446, 'n_words': 325}