import moment from 'moment';

export const GetFormatter = {
    INPUT: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    SEARCH: (val) => {
        if (!Array.isArray(val) || val.length === 0) return void 0;
        return { value: val[0].value, label: val[0].text };
    },
    TEXTAREA: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    UPLOAD: (val) => {
        if (!Array.isArray(val)) return void 0;
        return val.map((item) => {
            let size = 0;
            let text = item.text.replace(/\((\d+)\)$/, (s0, s1, s2) => {
                size = s1;
                return '';
            });
            return { value: item.value, name: text, size };
        });
    },
    CHECKBOX: (val, config) => {
        const { isMultiple } = config || {};
        if (isMultiple) {
            return Array.isArray(val)
                ? val.map((item) => {
                      return item.value;
                  })
                : [];
        }
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    SELECT: (val) => {
        // 处理成[{}]结构
        if (!Array.isArray(val) || val.length === 0) return undefined;
        const obj = val[0] || {};
        return { key: obj.value, label: obj.text };
    },
    MULTIPLE_SELECT: (val) => {
        // 处理成[{}]结构
        const obj = val[0] || {};
        return { key: obj.value, label: obj.text };
    },
    TEXT_SELECT: (val) => {
        return GetFormatter.SELECT(val);
    },
    RATE: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value || 0;
    },
    NUMBER: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    PERCENTAGE: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    DATE: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value ? moment(obj.value) : void 0;
    },
    LINK: (val) => {
        return Array.isArray(val) && val.length > 0 ? val : [];
    },
    MULTIPLE_SEARCH: (val) => {
        if (!Array.isArray(val)) return void 0;
        return val.map((item) => ({ value: item.value, label: item.text }));
    },
    TREE_SELECT: (val) => {
        debugger
        if (!Array.isArray(val)) return void 0;
        return val.map((item) => ({ value: item.value, label: item.text }));
    },
};
