import icon from 'part:@sanity/base/sync-icon'

export default {
  name: 'referenceTest',
  type: 'document',
  title: 'Reference test',
  description: 'Test cases for references',
  icon,
  fields: [
    {name: 'title', type: 'string'},
    {name: 'selfRef', type: 'reference', to: {type: 'referenceTest'}},
    {
      title: 'Reference to book or author',
      name: 'multiTypeRef',
      type: 'reference',
      to: [{type: 'book'}, {type: 'author'}],
    },
    {name: 'liveEditedDocument', type: 'reference', to: {type: 'thesis'}},
    {
      title: 'Book with decade filter',
      description: 'Reference will only search for books within given decade',
      name: 'filtered',
      type: 'object',
      validation: (Rule) =>
        Rule.custom((val) => (!val || val.decade ? true : 'Must have decade defined')),
      fields: [
        {
          title: 'Decade',
          description: 'eg. 1980, 1990, 2000',
          name: 'decade',
          type: 'number',
          validation: (Rule) =>
            Rule.required()
              .min(0)
              .max(3000)
              .custom((year) => {
                return year % 10 ? 'Must be a decade, eg use 1990 instead of 1994' : true
              }),
        },
        {
          name: 'book',
          title: 'Book reference',
          type: 'reference',
          to: {type: 'book'},
          options: {
            filter: (options) => {
              const decade = options.parent && options.parent.decade
              if (!decade) {
                return {filter: 'false'} // && false always returns no results :)
              }

              const minYear = Math.floor(decade / 10) * 10
              const maxYear = minYear + 9

              return {
                filter: 'publicationYear >= $minYear && publicationYear <= $maxYear',
                params: {minYear, maxYear},
              }
            },
          },
        },
      ],
    },
    {
      name: 'invalidFilter',
      title: 'Invalid filter',
      description: 'This will error when trying to search for anything',
      type: 'reference',
      to: {type: 'book'},
      options: {
        filter: '&',
      },
    },
    {
      name: 'asyncFilter',
      title: 'Async filter',
      type: 'reference',
      to: {type: 'book'},
      options: {
        filter: () =>
          Promise.resolve({filter: '$param == "something"', params: {param: 'something'}}),
      },
    },
    {
      name: 'refToTypeWithNoToplevelStrings',
      type: 'reference',
      to: {type: 'typeWithNoToplevelStrings'},
    },
    {
      name: 'someStrongRef',
      title: 'Strong reference (default)',
      type: 'reference',
      to: {type: 'author'},
    },
    {
      name: 'someWeakRef',
      title: 'Weak reference',
      type: 'reference',
      weak: true,
      to: {type: 'author'},
    },
    {
      name: 'anotherWeakRef',
      title: 'Another weak reference',
      type: 'reference',
      weak: true,
      to: {type: 'author'},
    },
    {
      name: 'array',
      type: 'array',
      of: [
        {
          type: 'reference',
          name: 'strongAuthorRef',
          title: 'A strong author ref',
          to: {type: 'author'},
        },
        {
          type: 'reference',
          name: 'weakAuthorRef',
          title: 'A weak author ref',
          weak: true,
          to: {type: 'author'},
        },
      ],
    },
  ],
  preview: {
    fields: {
      title: 'title',
      author0: 'array.0.name',
      author1: 'array.1.name',
    },
    prepare(val) {
      return {
        title: val.title,
        subtitle: [val.author0, val.author1].filter(Boolean).join(', '),
      }
    },
  },
}

@@ -232,24 +232,31 @@
 ing'%7D,%0A    %7B
+%0A      
 name: 'selfR
@@ -259,35 +259,120 @@
 elfRef',
- type: 'reference',
+%0A      title: 'Reference to self',%0A      type: 'reference',%0A      description: 'Some description',%0A     
  to: %7Bty
@@ -391,16 +391,22 @@
 ceTest'%7D
+,%0A    
 %7D,%0A    %7B
@@ -552,24 +552,459 @@
 '%7D%5D,%0A    %7D,%0A
+    %7B%0A      name: 'array',%0A      type: 'array',%0A      title: 'Array of refs and inline objects',%0A      of: %5B%0A        %7Btype: 'empty', title: 'Inline object'%7D,%0A        %7Btype: 'book', title: 'Inline book'%7D,%0A        %7B%0A          type: 'reference',%0A          title: 'Reference to either %22species%22 or %22empty%22 document',%0A          description: 'Some description',%0A          to: %5B%7Btype: 'species'%7D, %7Btype: 'empty'%7D%5D,%0A        %7D,%0A      %5D,%0A    %7D,%0A
     %7Bname: '
@@ -3556,32 +3556,50 @@
     name: 'array
+OfCustomReferences
 ',%0A      type: '

add an example of an array of objects + references