import {
    UPLOAD_MEDIA_REQUEST,
    UPLOAD_MEDIA_SUCCESS,
    UPLOAD_MEDIA_FAILURE,
    UPLOAD_MEDIA_PRES_REQUEST,
    UPLOAD_MEDIA_PRES_FAILURE,
    UPLOAD_MEDIA_PRES_SUCCESS,
    UPLOAD_MEDIA_CV_REQUEST,
    UPLOAD_MEDIA_CV_FAILURE,
    UPLOAD_MEDIA_CV_SUCCESS,
} from "../types";

export function media(state = {
}, action = {}) {
    switch (action.type) {
        case UPLOAD_MEDIA_REQUEST:
            return {
                ...state,
                loading: true
            };
        case UPLOAD_MEDIA_SUCCESS:
            return {
                ...state,
                ...action.res.data,
                loading: false
            };
        case UPLOAD_MEDIA_FAILURE:
            return {
                ...state,
                errors: action.errors,
                loading: false
            };
        case UPLOAD_MEDIA_PRES_REQUEST:
            return {
                ...state,
                loading: true
            };
        case UPLOAD_MEDIA_PRES_SUCCESS:
            return {
                ...state,
                ...action.res.data,
                loading: false
            };
        case UPLOAD_MEDIA_PRES_FAILURE:
            return {
                ...state,
                errors: action.errors,
                loading: false
            };
        case UPLOAD_MEDIA_CV_REQUEST:
            return {
                ...state,
                loading: true
            };
        case UPLOAD_MEDIA_CV_SUCCESS:
            return {
                ...state,
                ...action.res.data,
                loading: false
            };
        case UPLOAD_MEDIA_CV_FAILURE:
            return {
                ...state,
                errors: action.errors,
                loading: false
            };
        default:
            return state;
    }
}

export const mediaErrorsSelector = state => {return !!state.media.errors ?state.media.errors.response.data:null};

export const errorMessageSelector = (state) => {
    let err = mediaErrorsSelector(state)
    if (!!!err)
        return null;
    switch(err.code)
    {
        case 500:
            return {message: "Le téléversement de votre video à échoué."}
    }
}