import {SubscriptionCodeListActionTypes } from './susbcription-code-list.action.type'

const INITIAL_STATE = 
{
    inProgress: false,
    result: [],
    apiResponseStatus: -1,
    errorMessages: null,
    recordCount: 0,
    submissionInProgress: false,
    updateIndex: -1,
    reportInProgress: false,
    reportResult: null
}

const subscriptionCodeListReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case SubscriptionCodeListActionTypes.GET_SUBSCRIPTION_CODE_LIST_START:
            return{
                ...currentState,
                inProgress: true,

            }
        case SubscriptionCodeListActionTypes.GET_SUBSCRIPTION_CODE_LIST_SUCCESS:
            {
                /*
                *as for odata api this returns empty string for patients instead of array
                *hence safe type check
                */
            const { count, records } = action.payload
            // const downloaded = Array.isArray(action.payload) ? action.payload : []
            return{
                ...currentState,
                result: records,
                errorMessages: null,
                inProgress: false,
                recordCount: count
            }
        }
        case SubscriptionCodeListActionTypes.GET_SUBSCRIPTION_CODE_LIST_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                errorMessages: message,
                result:[],
                inProgress: false
            }
        }
        
        //Post request handling starts

        case SubscriptionCodeListActionTypes.SUBSCRIPTION_CODE_UPDATE_START:{
            const {indexOfRecord } = action.payload
            return {
                ...currentState,
                submissionInProgress: true,
                updateIndex: indexOfRecord,
                errorMessages: null
            }
        }
        case SubscriptionCodeListActionTypes.SUBSCRIPTION_CODE_UPDATE_SUCCESS:{
            const updatedResult = [...currentState.result]            
            // const {IsActive } = updatedResult[currentState.updateIndex]
            updatedResult[currentState.updateIndex] = action.payload

            return {
                ...currentState,
                submissionInProgress: false,
                updateIndex: -1,
                result: updatedResult,
                errorMessages: null
            }
        }

        case SubscriptionCodeListActionTypes.SUBSCRIPTION_CODE_UPDATE_FAILURE:{
            const {message} = action.payload
            return {
                ...currentState,
                submissionInProgress: false,
                errorMessages: message,
                updateIndex: -1,
        
            }
        }

        case SubscriptionCodeListActionTypes.SUBSCRIPTION_CODE_LIST_CLEAR_ERROR_STATE: {
            return {
                ...currentState,
                submissionInProgress: false,
                updateIndex: -1,
                errorMessages: null,
                inProgress: false
            }
        }

        case SubscriptionCodeListActionTypes.GET_SUBSCRIPTION_CODE_REPORT_START:{
            return{
                ...currentState,
                reportInProgress: true
            }
        }
        case SubscriptionCodeListActionTypes.GET_SUBSCRIPTION_CODE_REPORT_SUCCESS:{
            const {count, records} = action.payload
            return {
                ...currentState,
                reportInProgress: false,
                reportResult: records
            }
        }
        case SubscriptionCodeListActionTypes.GET_SUBSCRIPTION_CODE_REPORT_FAILURE:{
            const {message} = action.payload

            return {
                ...currentState,
                reportInProgress: false,
                reportResult: undefined,
                errorMessages: message
            }
        }

        case SubscriptionCodeListActionTypes.SUBSCRIPTION_CODE_LIST_RESET_STATE:
            return INITIAL_STATE

        default: {
            return currentState
        }
    }
}

export default subscriptionCodeListReducer