<commit_before>import {getParameterlessTemplatesBySchemaType} from '@sanity/initial-value-templates'
import {SchemaType, getDefaultSchema} from './parts/Schema'
import {isActionEnabled} from './parts/documentActionUtils'
import {structureClient} from './parts/Client'
import {SortItem} from './Sort'
import {SerializeError, HELP_URL} from './SerializeError'
import {SerializeOptions, Child} from './StructureNodes'
import {ChildResolver, ChildResolverOptions, ItemChild} from './ChildResolver'
import {InitialValueTemplateItem} from './InitialValueTemplateItem'
import {
  GenericListBuilder,
  BuildableGenericList,
  GenericList,
  GenericListInput,
} from './GenericList'
import {DocumentBuilder, getDefaultDocumentNode} from './Document'

const resolveTypeForDocument = (id: string): Promise<string | undefined> => {
  const query = '*[_id in [$documentId, $draftId]]._type'
  const documentId = id.replace(/^drafts\./, '')
  const draftId = `drafts.${documentId}`
  return structureClient
    .fetch(query, {documentId, draftId}, {tag: 'structure.resolve-type'})
    .then((types) => types[0])
}

const validateFilter = (spec: PartialDocumentList, options: SerializeOptions) => {
  const filter = spec.options!.filter.trim()

  if (['*', '{'].includes(filter[0])) {
    throw new SerializeError(
      `\`filter\` cannot start with \`${filter[0]}\` - looks like you are providing a query, not a filter`,
      options.path,
      spec.id,
      spec.title
    ).withHelpUrl(HELP_URL.QUERY_PROVIDED_FOR_FILTER)
  }

  return filter
}

const resolveDocumentChildForItem: ChildResolver = (
  itemId: string,
  options: ChildResolverOptions
): ItemChild | Promise<ItemChild> | undefined => {
  const parentItem = options.parent as DocumentList
  const type = parentItem.schemaTypeName || resolveTypeForDocument(itemId)
  return Promise.resolve(type).then((schemaType) =>
    schemaType
      ? getDefaultDocumentNode({schemaType, documentId: itemId})
      : new DocumentBuilder().id('editor').documentId(itemId).schemaType('')
  )<commit_msg>fix(structure): resolve schema type from template if possible<commit_after>import {
  getParameterlessTemplatesBySchemaType,
  getTemplateById,
} from '@sanity/initial-value-templates'
import {SchemaType, getDefaultSchema} from './parts/Schema'
import {isActionEnabled} from './parts/documentActionUtils'
import {structureClient} from './parts/Client'
import {SortItem} from './Sort'
import {SerializeError, HELP_URL} from './SerializeError'
import {SerializeOptions, Child} from './StructureNodes'
import {ChildResolver, ChildResolverOptions, ItemChild} from './ChildResolver'
import {InitialValueTemplateItem} from './InitialValueTemplateItem'
import {
  GenericListBuilder,
  BuildableGenericList,
  GenericList,
  GenericListInput,
} from './GenericList'
import {DocumentBuilder, getDefaultDocumentNode} from './Document'

const resolveTypeForDocument = (id: string): Promise<string | undefined> => {
  const query = '*[_id in [$documentId, $draftId]]._type'
  const documentId = id.replace(/^drafts\./, '')
  const draftId = `drafts.${documentId}`
  return structureClient
    .fetch(query, {documentId, draftId}, {tag: 'structure.resolve-type'})
    .then((types) => types[0])
}

const validateFilter = (spec: PartialDocumentList, options: SerializeOptions) => {
  const filter = spec.options!.filter.trim()

  if (['*', '{'].includes(filter[0])) {
    throw new SerializeError(
      `\`filter\` cannot start with \`${filter[0]}\` - looks like you are providing a query, not a filter`,
      options.path,
      spec.id,
      spec.title
    ).withHelpUrl(HELP_URL.QUERY_PROVIDED_FOR_FILTER)
  }

  return filter
}

const resolveDocumentChildForItem: ChildResolver = (
  itemId: string,
  options: ChildResolverOptions
): ItemChild | Promise<ItemChild> | undefined => {
  const parentItem = options.parent as DocumentList
  const template = options.params?.template ? getTemplateById(options.params.template) : undefined
  const type = template
    ? template.schemaType
    : parentItem.schemaTypeName || resolveTypeForDocument(itemId)

  return Promise.resolve(type).then((schemaType) =>
    schemaType
      ? getDefaultDocumentNode({schemaType, documentId: itemId})
      : new DocumentBuilder().id('editor').documentId(itemId).schemaType('')
  )
{'fraction_non_alphanumeric': 0.13020227853987446, 'fraction_numerical': 0.0013950244129272262, 'mean_word_length': 5.102127659574468, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '520434', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1227, 'n_words': 301}