import { ModelProperty, Property } from '../types/model.types' import { globalDefinitions } from './global' import { getAtomProperties } from './get-atom-properties' import { getResultModel, getResultAtom } from './get-string-result' export const createProperty = ( propertyName: string, property: ModelProperty, onUseRef?: (id: string) => void ): Property => { const { type, isArray } = property let output: Property = { schema: {}, getString: () => '', } switch (typeof type) { case 'string': output = getAtomProperties(propertyName, property, output) break case 'object': { const enumArray = Array.isArray(type.enum) ? type.enum : [type.enum] output.schema = { enum: enumArray as string[], } if (property.default !== undefined) { output.schema.default = property.default as string } output.getString = (parentPropertyName) => getResultAtom( propertyName, parentPropertyName?.length ? `${parentPropertyName}.${propertyName}` : propertyName, property.default, isArray ) break } case 'function': { if (!type.getSchema) { break } const childrenSchema = type.getSchema() if (childrenSchema.$id) { onUseRef?.(childrenSchema.$id) globalDefinitions.set(childrenSchema.$id, childrenSchema) } output.schema = { type: 'object', $ref: childrenSchema.$id, } output.getString = (parentPropertyName) => getResultModel({ propertyName: parentPropertyName ?? propertyName, isArray, defaultValue: property.default, }) break } } if (isArray) { output.schema = { type: 'array', items: output.schema, } } return output }