import { SubscriptionCodeOperationTypes} from './subscription-codes.action.types'

const INITIAL_STATE = 
{
    inProgress: false,
    result: null,
    apiResponseStatus: -1,
    errorMessages: null,
    fetchingInProgress: false
}

const subscriptionCodeReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case SubscriptionCodeOperationTypes.POST_SUBSCRIPTION_CODE_START:
            return{
                ...currentState,
                inProgress: true

            }
        case SubscriptionCodeOperationTypes.POST_SUBSCRIPTION_CODE_SUCCESS:
            return{
                ...currentState,
                result: action.payload,
                errorMessages: null,
                inProgress: false,

            }

        case SubscriptionCodeOperationTypes.POST_SUBSCRIPTION_CODE_FAILURE:
            const { message, apiResponseStatus} = action.payload
            return{
                ...currentState,
                errorMessages: message,
                apiResponseStatus: apiResponseStatus,
                inProgress: false
            }
        
        case SubscriptionCodeOperationTypes.SUBSCRIPTION_CODE_RESET_STATE:
            return{
                ...currentState,
                errorMessages: null,  
                fetchingInProgress: false,  
                inProgress: false,
                result: null          
            }


        default: {
            return currentState
        }
    }
}

export default subscriptionCodeReducer