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}
        },
        
    
        
    
        
    
        
        addressesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.label)
        },
        addressesOneListFormatter(val) {
            if (!val) return ''
            return val.label
        },
        addressesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.label}
            });
        },
        addressesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.label, id: val.id}
        },
        
    
        
        customersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        customersOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        customersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        customersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        suppliersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        suppliersOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        suppliersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        suppliersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        payment_termsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        payment_termsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        payment_termsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        payment_termsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        taxesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        taxesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        taxesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        taxesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        unitsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        unitsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        unitsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        unitsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        categoriesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        categoriesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        categoriesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        categoriesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.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}
        },
        
    
        
    
        
    
        
        price_listsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        price_listsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        price_listsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        price_listsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        price_list_itemsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.valid_from)
        },
        price_list_itemsOneListFormatter(val) {
            if (!val) return ''
            return val.valid_from
        },
        price_list_itemsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.valid_from}
            });
        },
        price_list_itemsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.valid_from, id: val.id}
        },
        
    
        
        sales_ordersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.order_number)
        },
        sales_ordersOneListFormatter(val) {
            if (!val) return ''
            return val.order_number
        },
        sales_ordersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.order_number}
            });
        },
        sales_ordersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.order_number, id: val.id}
        },
        
    
        
        sales_order_linesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.description)
        },
        sales_order_linesOneListFormatter(val) {
            if (!val) return ''
            return val.description
        },
        sales_order_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.description}
            });
        },
        sales_order_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.description, id: val.id}
        },
        
    
        
        purchase_ordersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.po_number)
        },
        purchase_ordersOneListFormatter(val) {
            if (!val) return ''
            return val.po_number
        },
        purchase_ordersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.po_number}
            });
        },
        purchase_ordersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.po_number, id: val.id}
        },
        
    
        
        purchase_order_linesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.description)
        },
        purchase_order_linesOneListFormatter(val) {
            if (!val) return ''
            return val.description
        },
        purchase_order_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.description}
            });
        },
        purchase_order_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.description, id: val.id}
        },
        
    
        
        goods_receiptsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.gr_number)
        },
        goods_receiptsOneListFormatter(val) {
            if (!val) return ''
            return val.gr_number
        },
        goods_receiptsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.gr_number}
            });
        },
        goods_receiptsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.gr_number, id: val.id}
        },
        
    
        
        goods_receipt_linesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.line_total)
        },
        goods_receipt_linesOneListFormatter(val) {
            if (!val) return ''
            return val.line_total
        },
        goods_receipt_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.line_total}
            });
        },
        goods_receipt_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.line_total, id: val.id}
        },
        
    
        
        shipmentsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.shipment_number)
        },
        shipmentsOneListFormatter(val) {
            if (!val) return ''
            return val.shipment_number
        },
        shipmentsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.shipment_number}
            });
        },
        shipmentsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.shipment_number, id: val.id}
        },
        
    
        
        shipment_linesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.quantity)
        },
        shipment_linesOneListFormatter(val) {
            if (!val) return ''
            return val.quantity
        },
        shipment_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.quantity}
            });
        },
        shipment_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.quantity, 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.description)
        },
        invoice_linesOneListFormatter(val) {
            if (!val) return ''
            return val.description
        },
        invoice_linesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.description}
            });
        },
        invoice_linesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.description, id: val.id}
        },
        
    
        
    
}
