/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2020
 */

import currency from 'currency.js';

import { ROUTES } from '../navigation';
import { PAYMENT_ACTIONS } from '../actions/PaymentActions';
import { WIZARD_ACTIONS } from '../actions/WizardActions';
import { selectPrescriptionTotal } from '../selectors/payment';

const initialState = () => ({
  transaction: null,
  patient: null,
  paymentAmount: currency(0),
  policy: null,
  paymentValid: true,
});

export const PaymentReducer = (state = initialState(), action) => {
  const { type } = action;

  switch (type) {
    case 'Navigation/NAVIGATE': {
      const { routeName, params } = action;

      if (routeName !== ROUTES.PRESCRIPTION) return state;
      const { transaction } = params;
      return { ...state, transaction };
    }

    case WIZARD_ACTIONS.NEXT_TAB: {
      const { transaction } = state;
      if (!transaction) return state;
      const paymentAmount = selectPrescriptionTotal({ payment: state });
      return { ...state, paymentAmount };
    }

    case PAYMENT_ACTIONS.CHOOSE_POLICY: {
      const { payload } = action;
      const { policy } = payload;

      return { ...state, policy };
    }

    case PAYMENT_ACTIONS.CHOOSE_PAYMENT_TYPE: {
      const { payload } = action;
      const { paymentType } = payload;

      return { ...state, paymentType };
    }

    case PAYMENT_ACTIONS.UPDATE_PAYMENT: {
      const { payload } = action;
      const { amount } = payload;

      return { ...state, paymentAmount: amount, creditOverflow: false, paymentValid: true };
    }

    case PAYMENT_ACTIONS.CREDIT_OVERFLOW: {
      return { ...state, creditOverflow: true, paymentValid: false };
    }

    default:
      return state;
  }
};

@@ -422,17 +422,26 @@
 y(0),%0A  
-p
+insuranceP
 olicy: n
@@ -467,16 +467,37 @@
 : true,%0A
+  paymentType: null,%0A
 %7D);%0A%0Aexp
@@ -1153,17 +1153,26 @@
 const %7B 
-p
+insuranceP
 olicy %7D 
@@ -1208,17 +1208,26 @@
 .state, 
-p
+insuranceP
 olicy %7D;

Add rename policy->insurancePolicy in PaymentReducer