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}
        },
        
    
        
        work_centersManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        work_centersOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        work_centersManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        work_centersOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        machinesManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        machinesOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        machinesManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        machinesOneListFormatterEdit(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}
        },
        
    
        
        units_of_measureManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.code)
        },
        units_of_measureOneListFormatter(val) {
            if (!val) return ''
            return val.code
        },
        units_of_measureManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.code}
            });
        },
        units_of_measureOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.code, id: val.id}
        },
        
    
        
        materialsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        materialsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        materialsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        materialsOneListFormatterEdit(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}
        },
        
    
        
        storage_locationsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.code)
        },
        storage_locationsOneListFormatter(val) {
            if (!val) return ''
            return val.code
        },
        storage_locationsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.code}
            });
        },
        storage_locationsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.code, 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}
        },
        
    
        
    
        
    
        
        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}
        },
        
    
        
    
        
        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}
        },
        
    
        
    
        
    
        
        production_lotsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.lot_number)
        },
        production_lotsOneListFormatter(val) {
            if (!val) return ''
            return val.lot_number
        },
        production_lotsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.lot_number}
            });
        },
        production_lotsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.lot_number, id: val.id}
        },
        
    
        
        qa_inspectionsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.inspection_number)
        },
        qa_inspectionsOneListFormatter(val) {
            if (!val) return ''
            return val.inspection_number
        },
        qa_inspectionsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.inspection_number}
            });
        },
        qa_inspectionsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.inspection_number, id: val.id}
        },
        
    
        
        qa_checklistsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.name)
        },
        qa_checklistsOneListFormatter(val) {
            if (!val) return ''
            return val.name
        },
        qa_checklistsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.name}
            });
        },
        qa_checklistsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.name, id: val.id}
        },
        
    
        
        qa_checklist_itemsManyListFormatter(val) {
            if (!val || !val.length) return []
            return val.map((item) => item.check_name)
        },
        qa_checklist_itemsOneListFormatter(val) {
            if (!val) return ''
            return val.check_name
        },
        qa_checklist_itemsManyListFormatterEdit(val) {
            if (!val || !val.length) return []
            return val.map((item) => {
              return {id: item.id, label: item.check_name}
            });
        },
        qa_checklist_itemsOneListFormatterEdit(val) {
            if (!val) return ''
            return {label: val.check_name, id: val.id}
        },
        
    
        
    
        
    
        
    
        
    
}
