text: var shared = require('./shared');
  var Processor = require('./processor');
  var Component = require("./component");
  var Cleanup = require("./cleanup");
  
  var MedicationInformation = Component.define("MedicationInformation")
  .templateRoot("2.16.840.1.113883.10.20.22.4.23")
  .fields([
    ["productName","0..1", "h:manufacturedMaterial/h:code", 
      shared.ConceptDescriptor.shall({valueSetOid: "2.16.840.1.113883.3.88.12.80.17"})],
    ["freeTextProductName","0..1", "h:manufacturedMaterial/h:code/h:originalText", shared.TextWithReference],
    //TODO: datatype?  ["manufacturer","0..1", "h:manufacturerOrganization", ??],
  ]);
  
  var MedicationActivity = Component.define("MedicationActivity")
  .templateRoot("2.16.840.1.113883.10.20.22.4.16")
  .fields([
    ["sourceIds","1..*", "h:id", shared.Identifier],
    ["deliveryMethod","0..1", "h:code", shared.ConceptDescriptor],
    ["route","0..1", "h:routeCode", shared.SimplifiedCode],
    ["site","0..1", "h:approachSiteCode", shared.ConceptDescriptor],
    ["administrationUnit","0..1", "h:administrationUnitCode", shared.ConceptDescriptor],
    ["times", "1..*", "h:effectiveTime", shared.EffectiveTime],
    ["medicationName", "1..1", "h:consumable/h:manufacturedProduct", MedicationInformation],
    ["freeTextSig","0..1", "h:text", shared.TextWithReference],
    ["dose","0..1", "h:doseQuantity", shared.PhysicalQuantity],
    ["rate","0..1", "h:rateQuantity", shared.PhysicalQuantity],
  ])
  .cleanupStep(Cleanup.extractAllFields(["medicationName"]))
  .cleanupStep(function(){
    // separate out two effectiveTimes
  
    // 1.  startDate --- endDate
    var range = this.js.times.filter(function(t){
      return -1 === ['PIVL_TS', 'EIVL_TS'].indexOf(t.js.xsitype);
    });
  
    // 2.  dosing interval
    var period= this.js.times.filter(function(t){
      return -1 !== ['PIVL_TS', 'EIVL_TS'].indexOf(t.js.xsitype);
    });
  
    delete this.js.times;
  
    if (range.length > 0) {
      this.js.dateRange = range[0];
    }
  
    if (period.length > 0) {
      this.js.dosePeriod = period[0].js.period;
    }
  });
  
  var MedActivityRx = MedicationActivity.define("Prescription")
  .withMood("INT")
  .uriBuilder({
    category:"entries",
    type:"medicationsPrescribed"
  });
  
  var MedActivityHx = MedicationActivity.define("MedActivityHx")
  .withMood("EVN")
  .uriBuilder({
    category:"entries",
    type:"medicationsReported"
  });
  
  exports.MedicationsSection = Component.define("MedicationsSection")
  .templateRoot(["2.16.840.1.113883.10.20.22.2.1", "2.16.840.1.113883.10.20.22.2.1.1"])
  .fields([
    ["medicationsPrescribed","0..*", MedActivityRx.xpath(), MedActivityRx],
    ["medicationsReported","0..*", MedActivityHx.xpath(), MedActivityHx],
  ])
  .uriBuilder({
    category: "sections",
    type: "medications"
  })
  .cleanupStep(Cleanup.ensureMutuallyExclusive([
    "medicationsPrescribed",
    "medicationsReported", 
  ]));
  