import { apiRequest } from 'platform/utilities/api';
import environment from 'platform/utilities/environment';
import { isVet360Configured } from 'platform/user/profile/vet360/util/local-vet360.js';
import {
  debtLettersSuccess,
  debtLettersSuccessVBMS,
} from '../utils/mockResponses';
import { deductionCodes } from '../const/deduction-codes';

export const DEBTS_FETCH_INITIATED = 'DEBTS_FETCH_INITIATED';
export const DEBTS_FETCH_SUCCESS = 'DEBTS_FETCH_SUCCESS';
export const DEBTS_FETCH_FAILURE = 'DEBTS_FETCH_FAILURE';
export const DEBTS_SET_ACTIVE_DEBT = 'DEBTS_SET_ACTIVE_DEBT';
export const DEBT_LETTERS_FETCH_INITIATED = 'DEBT_LETTERS_FETCH_INITIATED';
export const DEBT_LETTERS_FETCH_SUCCESS = 'DEBT_LETTERS_FETCH_SUCCESS';
export const DEBT_LETTERS_FETCH_FAILURE = 'DEBT_LETTERS_FETCH_FAILURE';

const fetchDebtLettersSuccess = debts => ({
  type: DEBTS_FETCH_SUCCESS,
  debts,
});

const fetchDebtLettersVBMSSuccess = debtLinks => ({
  type: DEBT_LETTERS_FETCH_SUCCESS,
  debtLinks,
});

const fetchDebtLettersFailure = () => ({ type: DEBTS_FETCH_FAILURE });
const fetchDebtLettersVBMSFailure = () => ({
  type: DEBT_LETTERS_FETCH_FAILURE,
});

const fetchDebtsInitiated = () => ({ type: DEBTS_FETCH_INITIATED });
const fetchDebtLettersInitiated = () => ({
  type: DEBT_LETTERS_FETCH_INITIATED,
});

export const setActiveDebt = debt => ({
  type: DEBTS_SET_ACTIVE_DEBT,
  debt,
});

export const fetchDebtLettersVBMS = () => async dispatch => {
  dispatch(fetchDebtLettersInitiated());
  try {
    const options = {
      method: 'GET',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'X-Key-Inflection': 'camel',
        'Source-App-Name': window.appName,
      },
    };
    const response = isVet360Configured()
      ? await apiRequest(`${environment.API_URL}/v0/debt_letters`, options)
      : await debtLettersSuccessVBMS();

    // Remove DMC  -  prefixing added by VBMS
    const filteredResponse = response.map(debtLetter => {
      if (debtLetter.typeDescription.includes('DMC - ')) {
        return {
          ...debtLetter,
          typeDescription: debtLetter.typeDescription.slice(6),
          date: new Date(debtLetter.receivedAt),
        };
      }
      return debtLetter;
    });

    return dispatch(fetchDebtLettersVBMSSuccess(filteredResponse));
  } catch (error) {
    return dispatch(fetchDebtLettersVBMSFailure());
  }
};

export const fetchDebtLetters = () => async dispatch => {
  dispatch(fetchDebtsInitiated());
  try {
    const options = {
      method: 'GET',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'X-Key-Inflection': 'camel',
        'Source-App-Name': window.appName,
      },
    };
    const response = isVet360Configured()
      ? await apiRequest(`${environment.API_URL}/v0/debts`, options)
      : await debtLettersSuccess();

    if (Object.keys(response).includes('error')) {
      return dispatch(fetchDebtLettersFailure());
    }

    const approvedDeductionCodes = Object.keys(deductionCodes);
    // remove any debts that do not have approved deductionCodes or
    // that have a current amount owed of 0
    const filteredResponse = response.debts
      .filter(res => approvedDeductionCodes.includes(res.deductionCode))
      .filter(debt => debt.currentAr > 0);

    // suppress VBMS call if they have dependent debt
    if (!response.hasDependentDebt) {
      dispatch(fetchDebtLettersVBMS());
    }
    return dispatch(fetchDebtLettersSuccess(filteredResponse));
  } catch (error) {
    return dispatch(fetchDebtLettersFailure());
  }
};

@@ -190,13 +190,71 @@
 t360
-.js';
+';%0Aimport recordEvent from 'platform/monitoring/record-event';%0A
 %0Aimp
@@ -2401,32 +2401,96 @@
 catch (error) %7B%0A
+    recordEvent(%7B event: 'bam-get-veteran-vbms-info-failed' %7D);%0A
     return dispa
@@ -2521,24 +2521,24 @@
 Failure());%0A
-
   %7D%0A%7D;%0A%0Aexpo
@@ -3059,24 +3059,89 @@
 'error')) %7B%0A
+      recordEvent(%7B event: 'bam-get-veteran-dmc-info-failed' %7D);%0A
       return
@@ -3518,24 +3518,291 @@
 ntAr %3E 0);%0A%0A
+    recordEvent(%7B%0A      event: 'bam-get-veteran-dmc-info-successful',%0A      'veteran-has-dependent-debt': response.hasDependentDebt,%0A    %7D);%0A%0A    if (filteredResponse.length %3E 0) %7B%0A      recordEvent(%7B 'number-of-current-debt-cards': filteredResponse.length %7D);%0A    %7D%0A
     // suppr
@@ -3983,32 +3983,32 @@
 eredResponse));%0A
-
   %7D catch (error
@@ -4007,24 +4007,87 @@
 h (error) %7B%0A
+    recordEvent(%7B event: 'bam-get-veteran-dmc-info-failed' %7D);%0A
     return d

Add analytics for debt letters i2 (#14777)