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}
        },
        
    
        
        tenantsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        tenantsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        tenantsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        tenantsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        farmsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        farmsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        farmsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        farmsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        trialsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        trialsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        trialsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        trialsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        trial_sitesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        trial_sitesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        trial_sitesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        trial_sitesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        varietiesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        varietiesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        varietiesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        varietiesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        trial_entriesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.entry_label)
        },
        trial_entriesOneListFormatter(val) {
            if (!val) return ''
            return val.entry_label
        },
        trial_entriesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.entry_label}
            });
        },
        trial_entriesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.entry_label, id: val.id}
        },
        
    
        
        plotsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.plot_code)
        },
        plotsOneListFormatter(val) {
            if (!val) return ''
            return val.plot_code
        },
        plotsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.plot_code}
            });
        },
        plotsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.plot_code, id: val.id}
        },
        
    
        
        plantsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.plant_tag)
        },
        plantsOneListFormatter(val) {
            if (!val) return ''
            return val.plant_tag
        },
        plantsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.plant_tag}
            });
        },
        plantsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.plant_tag, id: val.id}
        },
        
    
        
        observation_eventsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.summary)
        },
        observation_eventsOneListFormatter(val) {
            if (!val) return ''
            return val.summary
        },
        observation_eventsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.summary}
            });
        },
        observation_eventsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.summary, id: val.id}
        },
        
    
        
        traitsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        traitsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        traitsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        traitsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        observationsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.value_text)
        },
        observationsOneListFormatter(val) {
            if (!val) return ''
            return val.value_text
        },
        observationsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.value_text}
            });
        },
        observationsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.value_text, id: val.id}
        },
        
    
        
        media_assetsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.caption)
        },
        media_assetsOneListFormatter(val) {
            if (!val) return ''
            return val.caption
        },
        media_assetsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.caption}
            });
        },
        media_assetsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.caption, id: val.id}
        },
        
    
        
        report_templatesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        report_templatesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        report_templatesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        report_templatesOneListFormatterEdit(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}
        },
        
    
        
    
}