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}
        },
        
    
        
        companiesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.company_name)
        },
        companiesOneListFormatter(val) {
            if (!val) return ''
            return val.company_name
        },
        companiesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.company_name}
            });
        },
        companiesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.company_name, id: val.id}
        },
        
    
        
    
        
    
        
        vendorsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.vendor_name)
        },
        vendorsOneListFormatter(val) {
            if (!val) return ''
            return val.vendor_name
        },
        vendorsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.vendor_name}
            });
        },
        vendorsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.vendor_name, id: val.id}
        },
        
    
        
        warehousesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.warehouse_name)
        },
        warehousesOneListFormatter(val) {
            if (!val) return ''
            return val.warehouse_name
        },
        warehousesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.warehouse_name}
            });
        },
        warehousesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.warehouse_name, id: val.id}
        },
        
    
        
        product_categoriesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.category_name)
        },
        product_categoriesOneListFormatter(val) {
            if (!val) return ''
            return val.category_name
        },
        product_categoriesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.category_name}
            });
        },
        product_categoriesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.category_name, id: val.id}
        },
        
    
        
        productsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.product_name)
        },
        productsOneListFormatter(val) {
            if (!val) return ''
            return val.product_name
        },
        productsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.product_name}
            });
        },
        productsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.product_name, id: val.id}
        },
        
    
        
        purchase_requisitionsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.requisition_number)
        },
        purchase_requisitionsOneListFormatter(val) {
            if (!val) return ''
            return val.requisition_number
        },
        purchase_requisitionsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.requisition_number}
            });
        },
        purchase_requisitionsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.requisition_number, 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.grn_number)
        },
        goods_receiptsOneListFormatter(val) {
            if (!val) return ''
            return val.grn_number
        },
        goods_receiptsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.grn_number}
            });
        },
        goods_receiptsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.grn_number, id: val.id}
        },
        
    
        
    
        
        vendor_invoicesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.invoice_number)
        },
        vendor_invoicesOneListFormatter(val) {
            if (!val) return ''
            return val.invoice_number
        },
        vendor_invoicesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.invoice_number}
            });
        },
        vendor_invoicesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.invoice_number, id: val.id}
        },
        
    
        
    
        
    
        
        departmentsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.department_name)
        },
        departmentsOneListFormatter(val) {
            if (!val) return ''
            return val.department_name
        },
        departmentsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.department_name}
            });
        },
        departmentsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.department_name, id: val.id}
        },
        
    
        
        document_typesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.type_name)
        },
        document_typesOneListFormatter(val) {
            if (!val) return ''
            return val.type_name
        },
        document_typesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.type_name}
            });
        },
        document_typesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.type_name, id: val.id}
        },
        
    
        
        documentsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.document_number)
        },
        documentsOneListFormatter(val) {
            if (!val) return ''
            return val.document_number
        },
        documentsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.document_number}
            });
        },
        documentsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.document_number, id: val.id}
        },
        
    
        
    
        
        approval_workflowsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.workflow_name)
        },
        approval_workflowsOneListFormatter(val) {
            if (!val) return ''
            return val.workflow_name
        },
        approval_workflowsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.workflow_name}
            });
        },
        approval_workflowsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.workflow_name, id: val.id}
        },
        
    
        
        approval_stepsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.step_name)
        },
        approval_stepsOneListFormatter(val) {
            if (!val) return ''
            return val.step_name
        },
        approval_stepsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.step_name}
            });
        },
        approval_stepsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.step_name, id: val.id}
        },
        
    
        
    
        
    
        
    
}
