export const generateFromAJV = (schema) => {
    if (schema && schema.properties) {
        const obj = Object.entries(schema.properties).map(([key, value]) => {
            const innerSchema = value;
            // eslint-disable-next-line @typescript-eslint/no-use-before-define
            return generateSingleEntity(key, innerSchema);
        });
        return obj.filter(Boolean);
    }
    return [];
};
export const generateSingleEntity = (key, schema) => {
    const type = schema.type;
    if (!type && schema.enum) {
        return {
            type: 'select',
            view: 'select',
            name: key,
            title: key,
            enum: schema.enum.map((enumValue) => ({
                content: enumValue,
                value: enumValue,
            })),
        };
    }
    if (schema.oneOf) {
        return {
            type: 'oneOf',
            name: key,
            title: key,
            options: schema.oneOf.map((item) => {
                let properties;
                if (item.properties) {
                    properties = generateFromAJV(item);
                }
                else {
                    properties = [
                        generateSingleEntity('', {
                            ...item,
                            name: '',
                            title: item.optionName,
                        }),
                    ];
                }
                return {
                    value: item.optionName,
                    title: item.optionName,
                    properties: properties,
                };
            }),
        };
    }
    if (schema.anyOf) {
        return {
            type: 'anyOf',
            name: key,
            title: key,
            options: schema.anyOf.map((item) => {
                let properties;
                if (item.properties) {
                    properties = generateFromAJV(item);
                }
                else {
                    properties = [
                        generateSingleEntity('', {
                            ...item,
                            name: '',
                            title: item.optionName,
                        }),
                    ];
                }
                return {
                    value: item.optionName,
                    title: item.optionName,
                    properties: properties,
                };
            }),
        };
    }
    switch (type) {
        case 'string': {
            if (schema.inputType === 'textarea') {
                return {
                    type: 'textarea',
                    name: key,
                    title: key,
                };
            }
            if (schema.enum) {
                return {
                    type: 'select',
                    view: 'select',
                    name: key,
                    title: key,
                    enum: schema.enum.map((enumValue) => ({
                        content: enumValue,
                        value: enumValue,
                    })),
                };
            }
            return {
                type: 'text',
                name: key,
                title: key,
            };
        }
        case 'number': {
            return {
                type: 'number',
                name: key,
                title: key,
            };
        }
        case 'object': {
            return {
                type: 'object',
                name: key,
                title: key,
                properties: generateFromAJV(schema),
            };
        }
        case 'boolean': {
            return {
                type: 'boolean',
                name: key,
                title: key,
                properties: generateFromAJV(schema),
            };
        }
        case 'array': {
            if (schema.items.type === 'string') {
                return {
                    type: 'array',
                    name: key,
                    title: key,
                    properties: generateFromAJV(schema.items),
                    buttonText: 'Add',
                    arrayType: 'text',
                };
            }
            return {
                type: 'array',
                name: key,
                title: key,
                properties: generateFromAJV(schema.items),
                buttonText: 'Add',
                arrayType: 'object',
            };
        }
    }
    return undefined;
};
//# sourceMappingURL=form-generator.js.map
