export const formatStr = 'YYYY-MM-DD HH:mm:ss';
export const emptyModel = [{ text: '', value: '' }];

const emptyFormat = (value) => {};
export const SetFormatter = {
    INPUT: (event) => {
        const node = event.currentTarget || {};
        return [{ value: node.value, text: node.value }];
    },
    SEARCH: (val) => {
        if (!val || typeof val !== 'object') return val;
        if (typeof val === 'object') {
            const values = Array.isArray(val) ? val : [val];
            return values.map((item) => ({
                value: item.fieldValueValue || item.key,
                text: item.fieldValueName || item.label,
            }));
        }
    },
    TEXTAREA: (value) => {
        if (!value) {
            return emptyModel;
        }
        return [{ value: value, text: value }];
    },
    UPLOAD: (val) => {
        if (!val) return null;
        return val.map((item) => ({ value: item.value, text: item.name + '(' + item.size + ')' }));
    },
    CHECKBOX: (event) => {
        const node = event.currentTarget || {};
        return [{ value: node.value, text: node.value }];
    },
    SELECT: (val) => {
        // 处理成[{}]结构
        if (!val) return val;
        if (typeof val === 'object') {
            const values = Array.isArray(val) ? val : [val];
            return values.map((item) => ({ value: item.key, text: item.label }));
        }
        return val;
    },
    MULTIPLE_SELECT: (val) => {
        // 处理成[{}]结构
        if (!val) return val;
        if (typeof val === 'object') {
            const values = Array.isArray(val) ? val : [val];
            return values.map((item) => ({ value: item.key, text: item.label }));
        }
        return val;
    },
    RATE: (val) => {
        return [{ value: val, text: val }];
    },
    NUMBER: (val) => {
        return [{ value: val, text: val }];
    },
    PERCENTAGE: (event) => {
        const node = event.currentTarget || {};
        return [{ value: node.value, text: node.value }];
    },
    DATE: (val, config) => {
        if (!val) return val;
        const { format } = config;
        let setFormat = format || formatStr;
        //airTable中需对日期类数据处理，没有时分秒时补全为0
        if (setFormat.indexOf('H') === -1) {
            setFormat += ' 00';
        }
        if (setFormat.indexOf('m') === -1) {
            setFormat += ':00';
        }
        if (setFormat.indexOf('s') === -1) {
            setFormat += ':00';
        }
        val = val.format ? val.format(setFormat) : val;
        return [{ value: val, text: val }];
    },
    LINK: (event) => {
        const node = event.currentTarget || {};
        return [{ value: node.value, text: node.value }];
    },
    MULTIPLE_SEARCH: (val) => {
        if (!val || typeof val !== 'object') return val;
        if (typeof val === 'object') {
            const values = Array.isArray(val) ? val : [val];
            return values.map((item) => ({
                value: item.fieldValueValue || item.key,
                text: item.fieldValueName || item.label,
            }));
        }
    },
    TREE_SELECT: (val) => {
        if (!val || typeof val !== 'object') return val;
        if (typeof val === 'object') {
            const values = Array.isArray(val) ? val : [val];
            return values.map((item) => ({ value: item.value, text: item.label }));
        }
    },
    TEXT_SELECT: (val) => {
        // 处理成[{}]结构
        if (!val) return val;
        if (typeof val === 'object') {
            const values = Array.isArray(val) ? val : [val];
            return values.map((item) => ({ value: item.value, text: item.label }));
        }
        return val;
    },
};
