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}
        },
        
    
        
    
        
    
        
        tax_codesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        tax_codesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        tax_codesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        tax_codesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        contactsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.display_name)
        },
        contactsOneListFormatter(val) {
            if (!val) return ''
            return val.display_name
        },
        contactsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.display_name}
            });
        },
        contactsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.display_name, id: val.id}
        },
        
    
        
        productsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        productsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        productsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        productsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        warehousesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        warehousesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        warehousesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        warehousesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
    
        
        quotesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.quote_number)
        },
        quotesOneListFormatter(val) {
            if (!val) return ''
            return val.quote_number
        },
        quotesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.quote_number}
            });
        },
        quotesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.quote_number, id: val.id}
        },
        
    
        
        quote_linesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.line_description)
        },
        quote_linesOneListFormatter(val) {
            if (!val) return ''
            return val.line_description
        },
        quote_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.line_description}
            });
        },
        quote_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.line_description, id: val.id}
        },
        
    
        
        invoicesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.invoice_number)
        },
        invoicesOneListFormatter(val) {
            if (!val) return ''
            return val.invoice_number
        },
        invoicesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.invoice_number}
            });
        },
        invoicesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.invoice_number, id: val.id}
        },
        
    
        
        invoice_linesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.line_description)
        },
        invoice_linesOneListFormatter(val) {
            if (!val) return ''
            return val.line_description
        },
        invoice_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.line_description}
            });
        },
        invoice_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.line_description, id: val.id}
        },
        
    
        
        paymentsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.payment_reference)
        },
        paymentsOneListFormatter(val) {
            if (!val) return ''
            return val.payment_reference
        },
        paymentsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.payment_reference}
            });
        },
        paymentsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.payment_reference, id: val.id}
        },
        
    
        
        receiptsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.receipt_number)
        },
        receiptsOneListFormatter(val) {
            if (!val) return ''
            return val.receipt_number
        },
        receiptsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.receipt_number}
            });
        },
        receiptsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.receipt_number, 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}
        },
        
    
        
        tasksManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.title)
        },
        tasksOneListFormatter(val) {
            if (!val) return ''
            return val.title
        },
        tasksManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.title}
            });
        },
        tasksOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.title, id: val.id}
        },
        
    
        
    
        
    
        
    
        
    
}
