#!/usr/bin/env node

'use strict';

const _ = require('lodash');
const VDM = require('../../vdm');
const eUtils = require('../rpcEmulatorUtils');

const RPC_EMULATOR_MODEL = [
    {
        id: 'ORWDAL32_DEF',
        name: 'ORWDAL32 DEF',
        function: 'DEF^ORWDAL32',
        domain: 'ALLERGY',
        description: 'Returns default values and list sets for Allergy ordering dialog.',
        rpcType: 'READ',
        complexity: 'DIFFICULT',
        returnType: 'SINGLE VALUE',
        files: ['120.83, 120.84'],
        parameters: [],
        labels: ['META'],
        services: [],
        mvdm: {
            New_Person: ['division'],
            GMR_Allergy_Site_Parameters: ['top10Reactions'],
        },
        inputParameters: [],
        processRPCInvocation(rpcArgs) {
            const alTitle = ['~Allergy Types'];
            const reactTitle = ['~Reactions'];

            const allergyAndReactArr = ['iD^Drug',
                'iF^Food',
                'iO^Other',
                'iDF^Drug,Food',
                'iDO^Drug,Other',
                'iFO^Food,Other',
                'iDFO^Drug,Food,Other'];

            const natOfReactArr = ['~Nature of Reaction',
                'iA^Allergy',
                'iP^Pharmacological',
                'iU^Unknown'];

            const histSevArr = ['~Observ/Hist',
                'io^Observed',
                'ih^Historical',
                '~Severity',
                'i3^Severe',
                'i2^Moderate',
                'i1^Mild'];

            const allergy12084 = 'GMR_Allergy_Site_Parameters-120_84';

            // Get User ID, Division IEN
            const userDUZ = VDM.userId().split('-')[1];
            const userDivIen = _.get(eUtils.tryMVDMDescribe(`200-${userDUZ}`), 'division[0].division.id', '');

            // Retrieve Allergy Cross Ref. If can't find, use 1.
            let allergyValue = eUtils.getCrossReferenceList(allergy12084, 'SITE', userDivIen);
            if (_.isEmpty(allergyValue)) allergyValue = eUtils.getCrossReferenceList(allergy12084, 'B', 'HOSPITAL');
            if (_.isEmpty(allergyValue)) allergyValue = eUtils.getCrossReferenceList(allergy12084, '', 0);
            if (_.isEmpty(allergyValue)) allergyValue = 1;

            // Create TopTen Array first by returning full VDM obj
            const topTenObj = eUtils.tryMVDMDescribe(`120_84-${allergyValue}`);
            if (topTenObj.error) return [];
            const topTenArr = topTenObj.top10Reactions.map(el => ({
                label: el.label,
                ien: el.id.split('-')[1],
            })).map(el => (`i${el.ien}^${el.label}`));
            topTenArr.unshift('~Top Ten');

            // Concat all components together to create output string
            const finalArr = alTitle.concat(allergyAndReactArr, reactTitle,
            allergyAndReactArr, natOfReactArr, topTenArr, histSevArr);
            return finalArr;
        },

        toReturnValue(res) {
            return res;
        },
    },
];

module.exports.rpcEmulatorModel = RPC_EMULATOR_MODEL;
