// @todo: remove the following line when part imports has been removed from this file
///
import {ComponentType} from 'react'
import diffResolvers from 'all:part:@sanity/base/diff-resolver'
import {
ArraySchemaType,
Diff,
DiffComponent,
DiffComponentOptions,
DiffComponentResolver,
ObjectSchemaType,
SchemaType,
} from '../../types'
import {defaultComponents} from './defaultComponents'
export function resolveDiffComponent(
type: SchemaType,
parentSchemaType?: ArraySchemaType | ObjectSchemaType
): DiffComponent | DiffComponentOptions | undefined {
let itType: SchemaType | undefined = type
while (itType) {
const resolved =
itType.diffComponent || tryResolve(itType, parentSchemaType) || defaultComponents[itType.name]
if (resolved) {
return resolved as DiffComponent
}
itType = itType.type
}
return defaultComponents[type.jsonType] || undefined
}
function tryResolve(
schemaType: SchemaType,
parentSchemaType?: ArraySchemaType | ObjectSchemaType
): DiffComponent | DiffComponentOptions | undefined {
const resolvers = diffResolvers as DiffComponentResolver[]
let resolved: ComponentType | DiffComponentOptions | undefined
for (const resolver of resolvers) {
if (typeof resolver !== 'function') {
// eslint-disable-next-line no-console
console.error('Diff component resolver is not a function: ', resolver)
continue
}
resolved = resolver({schemaType, parentSchemaType})
if (resolved) {
return resolved as DiffComponent
}
}
return undefined
}