/**
 * Schema数据清洗工具
 * 从低代码表单schema中提取并清洗字段配置信息
 */
import { INPUT_TYPE, VALUE_TYPE, SelectType } from '../../types';
/**
 * 根据组件的schema获取对应的值类型
 * @param componentName 组件名称
 * @param options 组件配置选项
 * @returns 返回对应的值类型
 */
export function getValueTypeBySchema(componentName, options) {
    switch (componentName) {
        case 'Input':
            return VALUE_TYPE.string;
        case 'NumberPicker':
            return VALUE_TYPE.number;
        case 'Switch':
            return VALUE_TYPE.boolean;
        case 'RadioGroup':
            return VALUE_TYPE.string;
        case 'CheckboxGroup':
            return VALUE_TYPE.ListString;
        case 'TextArea':
            return VALUE_TYPE.string;
        case 'Rating':
            return VALUE_TYPE.number;
        case 'Select': {
            const mode = options === null || options === void 0 ? void 0 : options.mode;
            const useDetailValue = options === null || options === void 0 ? void 0 : options.useDetailValue;
            if (!mode || mode === 'single') {
                return useDetailValue ? VALUE_TYPE.object : VALUE_TYPE.string;
            }
            else if (mode === 'multiple' || mode === 'tags') {
                return useDetailValue ? VALUE_TYPE.ListObject : VALUE_TYPE.ListString;
            }
            break;
        }
        case 'Range':
            return VALUE_TYPE.number;
        case 'TimePicker': {
            const outputFormat = options === null || options === void 0 ? void 0 : options.outputFormat;
            return outputFormat ? VALUE_TYPE.string : VALUE_TYPE.number;
        }
        case 'RangeTimePicker': {
            const outputFormat = options === null || options === void 0 ? void 0 : options.outputFormat;
            return outputFormat ? VALUE_TYPE.ListString : VALUE_TYPE.ListNumber;
        }
        case 'DatePicker': {
            const outputFormat = options === null || options === void 0 ? void 0 : options.outputFormat;
            return outputFormat ? VALUE_TYPE.string : VALUE_TYPE.number;
        }
        case 'RangePicker': {
            const outputFormat = options === null || options === void 0 ? void 0 : options.outputFormat;
            return outputFormat ? VALUE_TYPE.ListString : VALUE_TYPE.ListNumber;
        }
        case 'CascaderSelect': {
            const mode = options === null || options === void 0 ? void 0 : options.multiple;
            if (mode) {
                return VALUE_TYPE.ListString;
            }
            return VALUE_TYPE.string;
        }
        case 'CnRange':
            return VALUE_TYPE.number;
        case 'TreeSelect': {
            const mode = options === null || options === void 0 ? void 0 : options.multiple;
            if (mode) {
                return VALUE_TYPE.ListString;
            }
            return VALUE_TYPE.string;
        }
        case 'CnBankInput':
            return VALUE_TYPE.string;
        case 'CnCurrencyAmountNumberPickerPro':
            return VALUE_TYPE.object;
        case 'CnBatchInput':
            return VALUE_TYPE.string;
        case 'RangeNumberPicker':
            return VALUE_TYPE.ListNumber;
        case 'Transfer':
            return VALUE_TYPE.ListString;
        case 'AutoComplete':
            return VALUE_TYPE.string;
        case 'CnArrayTable':
            return VALUE_TYPE.ListObject;
        case 'EmployeeSelect':
            return VALUE_TYPE.object;
        case 'DepartmentSelect':
            return VALUE_TYPE.object;
        default:
            return null;
    }
    return null;
}
/**
 * 处理枚举select
 * @param componentName 组件名称
 * @param enumDicts 枚举字典
 * @returns 处理后的枚举数组
 */
function processEnum({ componentName, enumDicts, }) {
    if (componentName === 'Switch' || componentName === 'CnSwitch') {
        return [
            {
                key: '是',
                value: true,
            },
            {
                key: '否',
                value: false,
            },
        ];
    }
    if (SelectType[componentName]) {
        return enumDicts || [];
    }
    return [];
}
/**
 * 从schema中提取字段列表
 * @param schema 表单schema对象
 * @returns 字段列表数组
 */
export function getLowCodeSchemaProps(schema) {
    const result = [];
    function traverse(obj, path = '', parentKey = '') {
        var _a;
        if (!obj || typeof obj !== 'object')
            return;
        // 检查当前节点是否是字段节点
        if (obj['x-component'] &&
            typeof obj['x-component'] === 'string' &&
            obj['x-component-props'] !== undefined &&
            ((_a = obj['x-data']) === null || _a === void 0 ? void 0 : _a.hidden) !== true) {
            result.push([parentKey, obj]);
        }
        // 递归遍历 properties
        if (obj.properties && typeof obj.properties === 'object') {
            Object.entries(obj.properties).forEach(([key, value]) => {
                const currentPath = path ? `${path}.${key}` : key;
                traverse(value, currentPath, key);
            });
        }
    }
    traverse(schema);
    return result;
}
/**
 * 处理字段列表，转换为标准格式
 * @param fieldList 字段列表
 * @param options 可选配置
 * @returns 清洗后的实体元数据数组
 */
export function processColumnList(fieldList, options) {
    const { configList = [] } = options || {};
    return fieldList === null || fieldList === void 0 ? void 0 : fieldList.reduce((acc, [name, cur]) => {
        const { title, 'x-component': componentName, 'x-component-props': componentProps, enum: enumDicts, } = cur || {};
        if (!INPUT_TYPE[componentName]) {
            return acc;
        }
        // // 仅支持静态数据源
        // if (componentProps?.dataOrigin && componentProps?.dataOrigin !== 'static') {
        //   return acc;
        // }
        const entityComponentProps = { ...componentProps };
        // 为 EmployeeSelect 组件添加 relateEmployeeMsgEnums 字段
        if (componentName === 'EmployeeSelect') {
            const configItem = configList.find((item) => item.name === name);
            if (configItem === null || configItem === void 0 ? void 0 : configItem.relateEmployeeMsgEnums) {
                entityComponentProps.relateEmployeeMsgEnums = configItem.relateEmployeeMsgEnums;
            }
        }
        // 为 RangeTimePicker 组件添加 relateTimeMsgEnums 字段
        if (componentName === 'RangeTimePicker') {
            const configItem = configList.find((item) => item.name === name);
            if (configItem === null || configItem === void 0 ? void 0 : configItem.relateTimeMsgEnums) {
                entityComponentProps.relateTimeMsgEnums = configItem.relateTimeMsgEnums;
            }
        }
        // 为 RangePicker 组件添加 relateDateMsgEnums 字段
        if (componentName === 'RangePicker') {
            const configItem = configList.find((item) => item.name === name);
            if (configItem === null || configItem === void 0 ? void 0 : configItem.relateDateMsgEnums) {
                entityComponentProps.relateDateMsgEnums = configItem.relateDateMsgEnums;
            }
        }
        const baseEntity = {
            entityKey: name,
            entityChineseKey: title,
            entityType: componentName,
            enumDicts: processEnum({ componentName, enumDicts }),
            entityValueType: getValueTypeBySchema(componentName, componentProps) || '',
            entityComponentProps,
        };
        // 为 CnArrayTable 添加 children 字段
        if (componentName === 'CnArrayTable' && (componentProps === null || componentProps === void 0 ? void 0 : componentProps.config)) {
            baseEntity.children = componentProps.config
                // 过滤掉 hidden 为 true 的字段
                .filter((configItem) => !((configItem === null || configItem === void 0 ? void 0 : configItem.hidden) === true))
                .map((configItem) => {
                var _a, _b, _c;
                const childEntityComponentProps = configItem.options ? { ...configItem.options } : {};
                if (configItem.componentName === 'EmployeeSelect') {
                    childEntityComponentProps.relateEmployeeMsgEnums = configItem === null || configItem === void 0 ? void 0 : configItem.relateEmployeeMsgEnums;
                    childEntityComponentProps.typeList = (_b = (_a = configItem === null || configItem === void 0 ? void 0 : configItem.options) === null || _a === void 0 ? void 0 : _a.__advancedConfig__) === null || _b === void 0 ? void 0 : _b.typeList;
                }
                if (configItem.componentName === 'RangeTimePicker') {
                    childEntityComponentProps.relateTimeMsgEnums = configItem === null || configItem === void 0 ? void 0 : configItem.relateTimeMsgEnums;
                }
                if (configItem.componentName === 'RangePicker') {
                    childEntityComponentProps.relateDateMsgEnums = configItem === null || configItem === void 0 ? void 0 : configItem.relateDateMsgEnums;
                }
                return {
                    entityKey: configItem.name,
                    entityChineseKey: configItem.label,
                    entityType: configItem.componentName,
                    enumDicts: processEnum({
                        componentName: configItem.componentName,
                        enumDicts: ((_c = configItem.options) === null || _c === void 0 ? void 0 : _c.dataSource) || configItem.enum,
                    }),
                    entityValueType: getValueTypeBySchema(configItem.componentName, configItem.options) || '',
                    entityComponentProps: childEntityComponentProps,
                };
            });
        }
        acc.push(baseEntity);
        return acc;
    }, []);
}
/**
 * 清洗schema数据的主函数
 * @param schema 表单schema对象
 * @param options 可选配置
 * @returns 清洗后的实体元数据数组
 */
export function cleanSchemaData(schema, options) {
    // 从schema中提取字段列表
    const fieldList = getLowCodeSchemaProps(schema);
    // 处理字段列表，转换为标准格式
    return processColumnList(fieldList, options);
}
// 导出所有类型
export { INPUT_TYPE, VALUE_TYPE, SelectType } from '../../types';
