var _ = require('lodash');

var pathToDate = {};
pathToDate['AllergyIntolerance'] = 'content.recordedDate';
pathToDate['Condition'] = 'content.dateAsserted';
pathToDate['Immunization'] = 'content.date';
pathToDate['MedicationPrescription'] = 'content.dateWritten';
pathToDate['Observation'] = 'content.issued';

module.exports.effectiveDate = function (entry) {
    var value = _.get(entry, pathToDate[entry.content.resourceType]);

    if (!value) {
        return undefined;
    }

    return new Date(Date.parse(value));
};

var pathToCoding = {};
pathToCoding['AllergyIntolerance'] = 'content.substance.coding';
pathToCoding['Condition'] = 'content.code.coding';
pathToCoding['Encounter'] = 'content.reason.coding';
pathToCoding['Immunization'] = 'content.vaccineType.coding';
pathToCoding['Medication'] = 'content.code.coding';
pathToCoding['Observation'] = 'content.name.coding';

module.exports.coding = function (entry) {
    return _.get(entry, pathToCoding[entry.content.resourceType]);
};

module.exports.isCodeable = function (entry) {
    var path = pathToCoding[entry.content.resourceType];
    return _.isNil(path) ? false : true;
};


module.exports.effectiveCode = function (entry) {
    var coding = exports.coding(entry) || [];

    return coding.length > 0 ? coding[0].code : undefined;
};