import { GarbageTypes } from "./GarbageTypes"

export default (list: any[]) => {

    let tmp = [];

    const contains = (val) => {
        for (const i of tmp) {
            if (val.targetType == i.targetType && val.itemType == i.itemType && val.itemName == i.itemName)
                return true;
        }
        return false;
    }

    list.forEach(i => {
        if (!contains(i))
            tmp.push(i)
    })

    list = tmp;

    tmp = [];

    const nameList = [];
    const contains2 = (val) => {
        for (const i of tmp) {
            if (val.itemName == i.itemName) {
                nameList.push(i.itemName);
                return true;
            }
        }
        return false;
    }

    list.forEach(i => {
        if (!contains2(i))
            tmp.push(i)
    })

    list = tmp;

    list.forEach(i=>{
        if(nameList.indexOf(i.itemName) !=-1) {
            i.targetType=i.itemType
        }
    })

    return list.map(i => {
        return {
            'targetName': getTargetName(i.targetType),
            'isCorrect': i.targetType == i.itemType,
            'itemName': i.itemName,
            'itemTypeName':getTargetName(i.itemType)
        }
    })
}


const getTargetName = (type: GarbageTypes) => {

    switch (type) {
        case GarbageTypes.recoverableWaste:
            return '可回收物'
            break;

        case GarbageTypes.harmfulWaste:
            return '有害垃圾'
            break;

        case GarbageTypes.wetWaste:
            return '湿垃圾'
            break;

        case GarbageTypes.dryWaste:
            return '其他垃圾'
            break;

        default:
            break;
    }
}