<commit_before>import client from 'part:@sanity/base/client'
// import {utils as previewUtils} from 'part:@sanity/base/preview'
import {unprefixType} from '../utils/unprefixType'
import schema from 'part:@sanity/base/schema'
import {uniq, flatten, compact} from 'lodash'

export function fetchSingle(id) {
  return client.getDocument(id).then(doc => unprefixType(doc))
}

export function fetch(type) {
  const toFieldTypes = type.to.map(toField => toField.type)
  const params = toFieldTypes.reduce((acc, toFieldType, i) => {
    acc[`toFieldType${i}`] = `${schema.name}.${toFieldType.name}`
    return acc
  }, {})

  const eqls = Object.keys(params).map(key => (
    `_type == $${key}`
  )).join(' || ')

  return client.fetch(`*[${eqls}]`, params)
    .then(response => response.map(unprefixType))
}

export function materializeReferences(referenceIds) {
  return Promise.all(referenceIds.map(fetchSingle))
}

export function materializeReference(id) {
  return materializeReferences([id]).then(res => res[0])
}

function quote(str) {
  return `"${str}"`
}

export function referenceSearch(textTerm, type) {

  const textFields = uniq(compact(flatten(
    type.to.map(refType =>
      refType.fields.map(field =>
        (field.type.name == 'string' ? field.name : null)
      )
    )
  )))

  const typeFilter = type.to.map(toField => `${schema.name}.${toField.type.name}`).map(quote)
  const terms = textTerm.split(/\s+/).map(quote)
  const constraints = `[${typeFilter.join(', ')}] include _type && (${textFields.join(', ')}) match (${terms.join(',')})`
  const query = `*[${constraints}]` // todo: see if its possible to use selection from previews here

  return client.fetch(query)
    .then(response => response.map(unprefixType))
}<commit_msg>[form-builder] Update reference search queries to match new syntax<commit_after>import client from 'part:@sanity/base/client'
// import {utils as previewUtils} from 'part:@sanity/base/preview'
import {unprefixType} from '../utils/unprefixType'
import schema from 'part:@sanity/base/schema'
import {uniq, flatten, compact} from 'lodash'

export function fetchSingle(id) {
  return client.getDocument(id).then(doc => unprefixType(doc))
}

export function fetch(type) {
  const toFieldTypes = type.to.map(toField => toField.type)
  const types = toFieldTypes.map(toFieldType => quote(`${schema.name}.${toFieldType.name}`))
  return client.fetch(`*[_type in [${types.join(', ')}]]`)
    .then(response => response.map(unprefixType))
}

export function materializeReferences(referenceIds) {
  return Promise.all(referenceIds.map(fetchSingle))
}

export function materializeReference(id) {
  return materializeReferences([id]).then(res => res[0])
}

function quote(str) {
  return `"${str}"`
}

export function referenceSearch(textTerm, type) {

  const textFields = uniq(compact(flatten(
    type.to.map(refType =>
      refType.fields.map(field =>
        (field.type.name == 'string' ? field.name : null)
      )
    )
  )))

  const typeFilter = type.to.map(toField => `${schema.name}.${toField.type.name}`).map(quote)
  const terms = textTerm.split(/\s+/).map(quote)
  const constraints = `_type in [${typeFilter.join(', ')}] && (${textFields.join(', ')}) match (${terms.join(',')})`
  const query = `*[${constraints}]` // todo: see if its possible to use selection from previews here

  return client.fetch(query)
    .then(response => response.map(unprefixType))
}
{'fraction_non_alphanumeric': 0.17227259432582626, 'fraction_numerical': 0.000584966364434045, 'mean_word_length': 4.690515806988353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '2555929', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1114, 'n_words': 234}