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}
        },
        
    
        
        suppliersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.supplier_name)
        },
        suppliersOneListFormatter(val) {
            if (!val) return ''
            return val.supplier_name
        },
        suppliersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.supplier_name}
            });
        },
        suppliersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.supplier_name, id: val.id}
        },
        
    
        
        customersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.customer_name)
        },
        customersOneListFormatter(val) {
            if (!val) return ''
            return val.customer_name
        },
        customersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.customer_name}
            });
        },
        customersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.customer_name, id: val.id}
        },
        
    
        
        plantsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.plant_name)
        },
        plantsOneListFormatter(val) {
            if (!val) return ''
            return val.plant_name
        },
        plantsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.plant_name}
            });
        },
        plantsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.plant_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}
        },
        
    
        
        storage_locationsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.location_code)
        },
        storage_locationsOneListFormatter(val) {
            if (!val) return ''
            return val.location_code
        },
        storage_locationsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.location_code}
            });
        },
        storage_locationsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.location_code, id: val.id}
        },
        
    
        
        units_of_measureManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.uom_code)
        },
        units_of_measureOneListFormatter(val) {
            if (!val) return ''
            return val.uom_code
        },
        units_of_measureManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.uom_code}
            });
        },
        units_of_measureOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.uom_code, id: val.id}
        },
        
    
        
        materialsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.material_name)
        },
        materialsOneListFormatter(val) {
            if (!val) return ''
            return val.material_name
        },
        materialsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.material_name}
            });
        },
        materialsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.material_name, id: val.id}
        },
        
    
        
        material_revisionsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.revision_code)
        },
        material_revisionsOneListFormatter(val) {
            if (!val) return ''
            return val.revision_code
        },
        material_revisionsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.revision_code}
            });
        },
        material_revisionsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.revision_code, id: val.id}
        },
        
    
        
        bomsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.bom_code)
        },
        bomsOneListFormatter(val) {
            if (!val) return ''
            return val.bom_code
        },
        bomsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.bom_code}
            });
        },
        bomsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.bom_code, id: val.id}
        },
        
    
        
    
        
        routingsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.routing_code)
        },
        routingsOneListFormatter(val) {
            if (!val) return ''
            return val.routing_code
        },
        routingsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.routing_code}
            });
        },
        routingsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.routing_code, id: val.id}
        },
        
    
        
        operationsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.operation_name)
        },
        operationsOneListFormatter(val) {
            if (!val) return ''
            return val.operation_name
        },
        operationsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.operation_name}
            });
        },
        operationsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.operation_name, id: val.id}
        },
        
    
        
        work_centersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.work_center_name)
        },
        work_centersOneListFormatter(val) {
            if (!val) return ''
            return val.work_center_name
        },
        work_centersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.work_center_name}
            });
        },
        work_centersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.work_center_name, id: val.id}
        },
        
    
        
        machinesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.machine_name)
        },
        machinesOneListFormatter(val) {
            if (!val) return ''
            return val.machine_name
        },
        machinesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.machine_name}
            });
        },
        machinesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.machine_name, id: val.id}
        },
        
    
        
    
        
    
        
    
        
        production_ordersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.order_number)
        },
        production_ordersOneListFormatter(val) {
            if (!val) return ''
            return val.order_number
        },
        production_ordersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.order_number}
            });
        },
        production_ordersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.order_number, id: val.id}
        },
        
    
        
        work_order_operationsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.status)
        },
        work_order_operationsOneListFormatter(val) {
            if (!val) return ''
            return val.status
        },
        work_order_operationsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.status}
            });
        },
        work_order_operationsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.status, id: val.id}
        },
        
    
        
        inventory_lotsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.lot_number)
        },
        inventory_lotsOneListFormatter(val) {
            if (!val) return ''
            return val.lot_number
        },
        inventory_lotsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.lot_number}
            });
        },
        inventory_lotsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.lot_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}
        },
        
    
        
    
        
        quality_standardsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.standard_name)
        },
        quality_standardsOneListFormatter(val) {
            if (!val) return ''
            return val.standard_name
        },
        quality_standardsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.standard_name}
            });
        },
        quality_standardsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.standard_name, id: val.id}
        },
        
    
        
        inspection_plansManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.plan_name)
        },
        inspection_plansOneListFormatter(val) {
            if (!val) return ''
            return val.plan_name
        },
        inspection_plansManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.plan_name}
            });
        },
        inspection_plansOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.plan_name, id: val.id}
        },
        
    
        
        inspectionsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.inspection_number)
        },
        inspectionsOneListFormatter(val) {
            if (!val) return ''
            return val.inspection_number
        },
        inspectionsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.inspection_number}
            });
        },
        inspectionsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.inspection_number, id: val.id}
        },
        
    
        
        nonconformancesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.ncr_number)
        },
        nonconformancesOneListFormatter(val) {
            if (!val) return ''
            return val.ncr_number
        },
        nonconformancesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.ncr_number}
            });
        },
        nonconformancesOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.ncr_number, id: val.id}
        },
        
    
        
    
        
    
        
    
}
