import {AssetSourceComponentProps} from 'sanity'
import type {ComponentType} from 'react'
import {Control} from 'react-hook-form'
import {FieldErrors} from 'react-hook-form'
import {ForwardRefExoticComponent} from 'react'
import {JSX as JSX_2} from 'react'
import {Plugin as Plugin_2} from 'sanity'
import {RefAttributes} from 'react'
import type {SanityAssetDocument} from '@sanity/client'
import type {SanityImageAssetDocument} from '@sanity/client'
import {SVGProps} from 'react'
import {UseFormRegister} from 'react-hook-form'
import * as z from 'zod'

declare type Asset = FileAsset | ImageAsset

declare type AssetFormData = z.infer<typeof assetFormSchema>

declare const assetFormSchema: z.ZodObject<
  {
    altText: z.ZodOptional<z.ZodString>
    creditLine: z.ZodOptional<z.ZodString>
    description: z.ZodOptional<z.ZodString>
    opt: z.ZodObject<
      {
        media: z.ZodObject<
          {
            tags: z.ZodNullable<
              z.ZodArray<
                z.ZodObject<
                  {
                    label: z.ZodString
                    value: z.ZodString
                  },
                  'strip',
                  z.ZodTypeAny,
                  {
                    label: string
                    value: string
                  },
                  {
                    label: string
                    value: string
                  }
                >,
                'many'
              >
            >
          },
          'strip',
          z.ZodTypeAny,
          {
            tags:
              | {
                  label: string
                  value: string
                }[]
              | null
          },
          {
            tags:
              | {
                  label: string
                  value: string
                }[]
              | null
          }
        >
      },
      'strip',
      z.ZodTypeAny,
      {
        media: {
          tags:
            | {
                label: string
                value: string
              }[]
            | null
        }
      },
      {
        media: {
          tags:
            | {
                label: string
                value: string
              }[]
            | null
        }
      }
    >
    originalFilename: z.ZodString
    title: z.ZodOptional<z.ZodString>
  },
  'strip',
  z.ZodTypeAny,
  {
    opt: {
      media: {
        tags:
          | {
              label: string
              value: string
            }[]
          | null
      }
    }
    originalFilename: string
    altText?: string | undefined
    creditLine?: string | undefined
    description?: string | undefined
    title?: string | undefined
  },
  {
    opt: {
      media: {
        tags:
          | {
              label: string
              value: string
            }[]
          | null
      }
    }
    originalFilename: string
    altText?: string | undefined
    creditLine?: string | undefined
    description?: string | undefined
    title?: string | undefined
  }
>

declare type CustomFields = {
  altText?: string
  description?: string
  opt?: {
    media?: {
      tags?: SanityReference[]
    }
  }
  title?: string
}

declare type DetailsProps = {
  formUpdating: boolean
  handleCreateTag: (title: string) => void
  control: Control<AssetFormData>
  errors: FieldErrors<AssetFormData>
  register: UseFormRegister<AssetFormData>
  allTagOptions: TagSelectOption[]
  assetTagOptions: TagSelectOption[] | null
  currentAsset: Asset
  creditLine?: {
    enabled: boolean
    excludeSources?: string | string[] | undefined
  }
}

declare type FileAsset = SanityAssetDocument &
  CustomFields & {
    _type: 'sanity.fileAsset'
  }

declare type ImageAsset = SanityImageAssetDocument &
  CustomFields & {
    _type: 'sanity.imageAsset'
    creditLine?: string
  }

export declare const media: Plugin_2<void | MediaToolOptions>

export declare const mediaAssetSource: {
  component: (props: AssetSourceComponentProps) => JSX_2
  icon: ForwardRefExoticComponent<
    Omit<SVGProps<SVGSVGElement>, 'ref'> & RefAttributes<SVGSVGElement>
  >
  name: string
  title: string
}

export declare type MediaToolOptions = {
  maximumUploadSize?: number
  components?: {
    details?: ComponentType<
      DetailsProps & {
        renderDefaultDetails: (props: DetailsProps) => JSX_2.Element
      }
    >
  }
  creditLine: {
    enabled: boolean
    excludeSources?: string | string[]
  }
  directUploads?: boolean
}

declare type SanityReference = {
  _ref: string
  _type: 'reference'
  _weak?: boolean
}

declare const tagOptionSchema: z.ZodObject<
  {
    label: z.ZodString
    value: z.ZodString
  },
  'strip',
  z.ZodTypeAny,
  {
    label: string
    value: string
  },
  {
    label: string
    value: string
  }
>

declare type TagSelectOption = z.infer<typeof tagOptionSchema>

export {}
