import { UPDATE_JOB_FIELD, UPDATE_VEHICLE_FIELD } from './example-actions'

import { job } from './job'

const vehicles = [
    {
        id: '1',
        label: 'Push bike',
        documentLabel: 'A4',
        maxWeight: 5,
        value: 'bicycle',
        img: 'motorbike',
    },
    {
        id: '2',
        label: 'Motor bike',
        documentLabel: 'A4 Box',
        maxWeight: 10,
        value: 'motorbike',
        img: 'motorbike',
    },
]

const initialState = { job, vehicles, selectedVehicle: vehicles[1] }

export const exampleReducer = (state = initialState, action: any) => {

    switch (action.type) {
        case UPDATE_JOB_FIELD:
            return updateJobField(state, action)
        case UPDATE_VEHICLE_FIELD:
            return updateVehicleField(state, action)
        default:
            return state
    }
}

const updateJobField = (state: any, action: any) => ({
    ...state,
    job: {
        ...state.job,
        [action.name]: action.field.value,
    },
})

const updateVehicleField = (state: any, action: any) => ({
    ...state,
    selectedVehicle: action.field.value,
})
