import dayjs from 'dayjs';
import _ from 'lodash';

export default {
    filesFormatter(arr) {
        if (!arr || !arr.length) return [];
        return arr.map((item) => item);
    },
    imageFormatter(arr) {
        if (!arr || !arr.length) return []
        return arr.map(item => ({
            publicUrl: item.publicUrl || ''
        }))
    },
    oneImageFormatter(arr) {
        if (!arr || !arr.length) return ''
        return arr[0].publicUrl || ''
    },
    dateFormatter(date) {
        if (!date) return ''
        return dayjs(date).format('YYYY-MM-DD')
    },
    dateTimeFormatter(date) {
        if (!date) return ''
        return dayjs(date).format('YYYY-MM-DD HH:mm')
    },
    booleanFormatter(val) {
        return val ? 'Yes' : 'No'
    },
    dataGridEditFormatter(obj) {
        return _.transform(obj, (result, value, key) => {
            if (_.isArray(value)) {
                result[key] = _.map(value, 'id');
            } else if (_.isObject(value)) {
                result[key] = value.id;
            } else {
                result[key] = value;
            }
        });
    },
    
        
        usersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.firstName)
        },
        usersOneListFormatter(val) {
            if (!val) return ''
            return val.firstName
        },
        usersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.firstName}
            });
        },
        usersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.firstName, id: val.id}
        },
        
    
        
        rolesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        rolesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        rolesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        rolesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        permissionsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        permissionsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        permissionsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        permissionsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        organizationsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        organizationsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        organizationsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        organizationsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        projectsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        projectsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        projectsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        projectsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        project_phasesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.gate_notes)
        },
        project_phasesOneListFormatter(val) {
            if (!val) return ''
            return val.gate_notes
        },
        project_phasesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.gate_notes}
            });
        },
        project_phasesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.gate_notes, id: val.id}
        },
        
    
        
    
        
        ore_bodiesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        ore_bodiesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        ore_bodiesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        ore_bodiesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        samplesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.sample_code)
        },
        samplesOneListFormatter(val) {
            if (!val) return ''
            return val.sample_code
        },
        samplesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.sample_code}
            });
        },
        samplesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.sample_code, id: val.id}
        },
        
    
        
    
        
        laboratoriesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        laboratoriesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        laboratoriesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        laboratoriesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        test_type_schemasManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.test_type)
        },
        test_type_schemasOneListFormatter(val) {
            if (!val) return ''
            return val.test_type
        },
        test_type_schemasManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.test_type}
            });
        },
        test_type_schemasOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.test_type, id: val.id}
        },
        
    
        
        metallurgical_testsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.lab_reference)
        },
        metallurgical_testsOneListFormatter(val) {
            if (!val) return ''
            return val.lab_reference
        },
        metallurgical_testsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.lab_reference}
            });
        },
        metallurgical_testsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.lab_reference, id: val.id}
        },
        
    
        
    
        
        flowsheetsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        flowsheetsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        flowsheetsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        flowsheetsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        equipment_itemsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.model)
        },
        equipment_itemsOneListFormatter(val) {
            if (!val) return ''
            return val.model
        },
        equipment_itemsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.model}
            });
        },
        equipment_itemsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.model, id: val.id}
        },
        
    
        
        flowsheet_nodesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        flowsheet_nodesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        flowsheet_nodesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        flowsheet_nodesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
    
        
    
        
        economic_modelsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        economic_modelsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        economic_modelsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        economic_modelsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
    
        
    
        
    
        
    
        
    
        
        commentsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.entity_key)
        },
        commentsOneListFormatter(val) {
            if (!val) return ''
            return val.entity_key
        },
        commentsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.entity_key}
            });
        },
        commentsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.entity_key, id: val.id}
        },
        
    
        
    
        
    
        
    
        
    
        
    
}
