import { normalizeSelectValue } from './normalize-select-value';
import { normalizeCascaderAndTreeValue } from './normalize-cascader-and-tree-value';
import { normalizeTimeValue } from './normalize-time-value';
import { normalizeDateValue } from './normalize-date-value';
import { normalizeEmployeeValue } from './normalize-employee-value';
import { normalizeDepartmentValue } from './normalize-department-value';
import { normalizeInputValue } from './normalize-input-value';
import { normalizeNumberValue } from './normalize-number-value';
import { normalizeBankInputValue } from './normalize-bank-input-value';
/**
 * 根据组件类型和 props 配置，标准化 token 的 value
 * @param token - translate 接口返回的 token
 * @param schema - Formily schema 配置
 * @returns 标准化后的 token
 */
export function normalizeTokenValue(token, schema) {
    if (token.queryType !== 'STRUCTURED' || !token.columnName) {
        return token;
    }
    const fieldConfig = schema[token.columnName];
    if (!fieldConfig) {
        return token;
    }
    const columnType = token.columnType || fieldConfig['x-component'];
    const componentProps = fieldConfig['x-component-props'] || {};
    // 根据组件类型进行标准化处理
    let normalizedValue = token.value;
    switch (columnType) {
        case 'Input':
        case 'CnInput':
        case 'TextArea':
        case 'CnTextArea':
        case 'CnBatchInput':
            normalizedValue = normalizeInputValue(token.value, componentProps);
            break;
        case 'CnBankInput':
            normalizedValue = normalizeBankInputValue(token.value);
            break;
        case 'Select':
        case 'CnAsyncSelect':
        case 'RadioGroup':
        case 'CnRadioGroup':
        case 'CnComplexRadio':
        case 'CheckboxGroup':
        case 'CnCheckboxGroup':
        case 'CnComplexCheckbox':
            normalizedValue = normalizeSelectValue(token.value, componentProps);
            break;
        case 'CascaderSelect':
        case 'CnCascaderSelect':
        case 'TreeSelect':
        case 'CnTreeSelect':
            normalizedValue = normalizeCascaderAndTreeValue(token.value, componentProps);
            break;
        case 'TimePicker':
        case 'CnTimePickerPro':
        case 'RangeTimePicker':
        case 'CnRangeTimePickerPro':
            normalizedValue = normalizeTimeValue(token.value, componentProps);
            break;
        case 'DatePicker':
        case 'CnDatePickerPro':
        case 'RangePicker':
        case 'CnRangeDatePickerPro':
            normalizedValue = normalizeDateValue(token.value, componentProps);
            break;
        case 'EmployeeSelect':
        case 'CnEmployeeSelect':
            // 传入 options，以便在标准化时设置 nickName
            normalizedValue = normalizeEmployeeValue(token.value, componentProps, token.options);
            break;
        case 'DepartmentSelect':
        case 'CnDepartmentSelect':
            normalizedValue = normalizeDepartmentValue(token.value, componentProps);
            break;
        case 'NumberPicker':
        case 'CnNumberPicker':
        case 'CnPercentInput':
        case 'CnRangeNumnerPicker':
        case 'CnProgress':
        case 'CnRating':
            normalizedValue = normalizeNumberValue(token.value, componentProps);
            break;
        default:
            // 其他组件类型保持原样
            // TODO: 地址、币种金额、开关
            break;
    }
    return {
        ...token,
        value: normalizedValue,
    };
}
