import moment from 'moment';
import { isNumber } from '../../../utils/utils';
import { accMul } from '../../../utils/calculate';
import { checkoutFileType } from '../extra/upload/utils';

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 undefined;
        return { value: val[0].value, label: val[0].text };
    },
    TEXTAREA: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    TEXTLINK: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value;
    },
    UPLOAD: (val) => {
        if (!Array.isArray(val)) return undefined;
        return val.map((item) => {
            let size = 0;
            const text = item.text.replace(/\((\d+)\)$/, (s0, s1, s2) => {
                size = s1;
                return '';
            });
            return { value: item.value, name: text, size, ...checkoutFileType(item.value) };
        });
    },
    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) => {
        // 处理成[{}]结构
        if (!Array.isArray(val) || val.length === 0) return undefined;
        return val.map((item) => {
            return { key: item.value, label: item.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, config) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        const { precision } = config;
        if (precision >= 0 && isNumber(obj.value)) {
            return Number(obj.value).toFixed(precision);
        }
        return obj.value;
    },
    PERCENTAGE: (val, config) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        if (isNumber(obj.value)) {
            return `${accMul(obj.value, 100).toFixed(config.precision)}%`;
        }
        return undefined;
    },
    DATE: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        return obj.value ? moment(obj.value) : undefined;
    },
    DATERANGE: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        const newVal = obj.value ? obj.value.split('~') : [];
        const [startTime, endTime] = newVal;
        return [startTime && moment(startTime), endTime && moment(endTime)];
    },
    LINK: (val) => {
        return Array.isArray(val) && val.length > 0 ? val : [];
    },
    MULTIPLE_SEARCH: (val) => {
        if (!Array.isArray(val)) return undefined;
        return val.map((item) => {
            return { value: item.value, label: item.text };
        });
    },
    TREE_SELECT: (val) => {
        if (!Array.isArray(val)) return undefined;
        return val.map((item) => {
            return { value: item.value, label: item.text };
        });
    },
    CASCADER: (val) => {
        const obj = Array.isArray(val) && val.length > 0 ? val[0] : {};
        if (typeof obj.value === 'string') {
            return obj.value.split('-');
        }
        return obj.value;
    },
};
