export function MarchReducer(datas, action) {
    switch (action.type) {
        case 'ADD':
            return [
                ...datas,
                action.payload
            ]
        case 'ADD_ALL':
            return [
                ...datas,
                ...action.payload
            ]
        case 'REPLACE_ALL':
            return [
                ...action.payload
            ]
        case 'REPLACE_TREE':
            return [
                ...datas.filter(data => data.categorie_ord !== -42),
                ...action.payload
            ]
        case 'CLEAR':
            return []
        case 'REMOVE':
            return datas.filter(data => data.id !== action.payload)
        case 'REMOVE_TREE':
            return datas.filter(data => data.categorie_ord !== -42)
        case 'UPDATE_OR_ADD':
            const index = datas.findIndex(data => data.id === action.payload.id)
            if (index === -1) {
                return [
                    ...datas,
                    action.payload
                ]
            } else {
                datas[index] = action.payload
                return [...datas]
            }

        case 'UPDATE_CARDBOARD':
            const idx = datas.findIndex(data => data.id === action.payload.match_id)
            if (idx === -1)
                return datas // Do nothing
            const data = datas[idx]
            const tmp = data.cardboard?.find(c => c.comb_id === action.payload.comb_id)
            if (tmp) {
                tmp.red = action.payload.red
                tmp.yellow = action.payload.yellow
            } else {
                if (!data.cardboard)
                    data.cardboard = []
                data.cardboard.push(action.payload)
            }
            return [...datas]
        case 'SORT':
            return datas.sort(action.payload)
        case 'REORDER':
            const oldIndex = datas.findIndex(data => data.id === action.payload.id)
            if (oldIndex === -1)
                return datas // Do nothing
            if (datas[oldIndex].categorie_ord === action.payload.pos)
                return [...datas] // Do nothing

            const oldPos = datas[oldIndex].categorie_ord
            const newPos = action.payload.pos

            const new_datas = []
            for (const data of datas) {
                if (data.id === action.payload.id) {
                    data.categorie_ord = newPos
                } else {
                    if (oldPos < newPos) {
                        // Moving down
                        if (data.categorie_ord > oldPos && data.categorie_ord <= newPos) {
                            data.categorie_ord -= 1
                        }
                    } else if (oldPos > newPos) {
                        // Moving up
                        if (data.categorie_ord < oldPos && data.categorie_ord >= newPos) {
                            data.categorie_ord += 1
                        }
                    }
                }
                new_datas.push(data)
            }
            return new_datas
        default:
            throw new Error()
    }
}
