837P mirth – I am trying to transform patient info when subscriber is not the patient along with some claims when subscriber is the patient

I am trying to transform patient info when subscriber is not the patient along with some claims when subscriber is the patient. I came across another challenge when subscriber is not the patient or subscriber has dependent. So far I tried below code and few other variations. This code gives me all the subscribers for all claims and patient with qualifier QC. I do not need subscriber name TFirst6 when claim has dependent “QC_TFirst6”. Sample message attached. Any advice is greatly appreciated. I tried deleting subscriber when subscriber is not the patient
Processed sample file
[“TFirst1″,”TFirst2″,”TFirst3″,”TFirst4″,”TFirst5″,”TFirst6″,”QC_TFirst6″,”TFirst7″,”TFirst8″,”TFirst9″,”TFirst10″,”TFirst11″,”TFirst12″,”TFirst13″,”TFirst14″,”TFirst15”]

Expected results

[“TFirst1″,”TFirst2″,”TFirst3″,”TFirst4″,”TFirst5″,”QC_TFirst6″,”TFirst7″,”TFirst8″,”TFirst9″,”TFirst10″,”TFirst11″,”TFirst12″,”TFirst13″,”TFirst14″,”TFirst15”]

var PatientFirstName = new Array();
var counter = 0;

for(var i = 0; i < msg['SBR'].length(); i++) {

        
        
                for each(nm1 in getSegmentsAfter(msg, msg['SBR'][i], 'NM1', true)) {
                    //msgNM2 = (nm1['NM1.01']['NM1.01.1'].toString());

                            if (nm1['NM1.01']['NM1.01.1'].toString() == 'QC') {
                                PatientFirstName[counter] = (nm1['NM1.04']['NM1.04.1'].toString());
                                counter += 1;
                            channelMap.put("PatientFirstName",PatientFirstName);}
                                
                            else if (nm1['NM1.01']['NM1.01.1'].toString() == 'IL') {
                                
                                
                                PatientFirstName[counter] = (nm1['NM1.04']['NM1.04.1'].toString());
                                counter += 1;
                            channelMap.put("PatientFirstName",PatientFirstName);
                            logger.info("PatientFirstName is=: " + PatientFirstName); 
                }}}