import { isNumber } from '@/apollo-table/utils/utils';

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

export const SetFormatter = {
    INPUT: (value) => {
        if (!value) {
            return emptyModel;
        }
        return [{ value: value, text: value }];
    },
    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: (value) => {
        if (!value) {
            return emptyModel;
        }
        if (Array.isArray(value)) {
            return value.map((item) => {
                return { value: item, text: item };
            });
        }
        return [{ value: value, text: value }];
    },
    SELECT: (val) => {
        // 处理成[{}]结构
        if (!val || typeof val !== 'object') return emptyModel;
        const values = Array.isArray(val) ? val : [val];
        return values.map((item) => ({ value: item.key, text: item.label }));
    },
    MULTIPLE_SELECT: (val) => {
        return SetFormatter.SELECT(val);
    },
    TEXT_SELECT: (val) => {
        return SetFormatter.SELECT(val);
    },
    SEARCH: (val) => {
        if (!val || typeof val !== 'object') return emptyModel;
        const values = Array.isArray(val) ? val : [val];
        return values.map((item) => ({
            value: item.fieldValueValue || item.key,
            text: item.fieldValueName || item.label,
        }));
    },
    MULTIPLE_SEARCH: (val) => {
        return SetFormatter.SEARCH(val);
    },
    RATE: (val) => {
        return [{ value: val, text: val }];
    },
    NUMBER: (val) => {
        if (!isNumber(val)) return emptyModel;
        return [{ value: val, text: val }];
    },
    PERCENTAGE: (val) => {
        if (!isNumber(val)) return emptyModel;
        val = val / 100;
        return SetFormatter.NUMBER(val);
    },
    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: (val) => {
        return Array.isArray(val) && val.length > 0 ? val : [emptyModel];
    },
    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 }));
        }
    },
    CASCADER: (val, config, selectedOptions) => {
        if (!val) return val;
        if (typeof val === 'object') {
            const {
                fieldNames: { label },
            } = config;
            const values = Array.isArray(val) ? val.join('-') : val;
            let labels = '';
            if (Array.isArray(selectedOptions)) {
                labels = selectedOptions
                    .reduce((pre, cur) => {
                        pre.push(cur[label]);
                        return pre;
                    }, [])
                    .join('-');
            }
            return [{ value: values, text: labels }];
        }
        return val;
    },
};
