"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MacroDescription = void 0;

var _preact = require("preact");

var _classnames = _interopRequireDefault(require("classnames"));

var _object = require("../utils/object");

var _vitalTypes = require("../utils/vital-types");

var _airwayTypes = require("../utils/airway-types");

var _macro = require("../utils/macro");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const DrugCategory = ({
  title,
  drugs,
  baseProp,
  itemsToHide,
  className
}) => (0, _preact.h)("div", {
  className: className
}, (0, _preact.h)(Title, null, title), drugs.map((drug, index) => {
  const {
    name
  } = drug,
        rest = _objectWithoutProperties(drug, ["name"]);

  const basePath = "".concat(baseProp, ".[").concat(index, "]");
  const array = (0, _macro.macroToItemArrayBase)((0, _object.omit)(rest, ['id', ...itemsToHide]), basePath, _macro.macroDrugLabelMap);
  return (0, _preact.h)(CategoryEntry, {
    path: basePath,
    name: name,
    key: "drug_".concat(index),
    items: array
  });
}));

const CategoryEntry = ({
  name,
  items
}) => {
  return (0, _preact.h)("div", {
    className: "pl1"
  }, (0, _preact.h)("h3", {
    className: "f6 mv0 di lh-solid mr1"
  }, name, items.length > 0 ? ':' : ''), (0, _preact.h)("span", {
    className: "f6"
  }, items.map(item => "".concat(item.label, ": ").concat(item.value)).join(', ')));
};

const ItemEntry = ({
  item,
  className
}) => (0, _preact.h)("div", {
  className: (0, _classnames.default)('f6', className)
}, item.label, ": ", item.value);

const Title = ({
  children,
  level = '1',
  className
}) => (0, _preact.h)("h4", {
  className: (0, _classnames.default)('f6', level === '1' && 'bb b--gray mb1 mt0', level === '2' && 'mv0', className)
}, children);
/** @param {{ macro: Macro }} */


const MacroDescription = (_ref) => {
  let {
    macro
  } = _ref,
      rest = _objectWithoutProperties(_ref, ["macro"]);

  if (!macro) {
    return null;
  }

  const items = (0, _macro.macroToItemArrayBase)(macro.copied, 'copied', _macro.macroLabelMap);
  const vitalItems = (0, _macro.macroToItemArrayBase)(macro.vitalTypes || {}, 'vitalTypes', _vitalTypes.vitalsNameMap); // re-use same labeling mechanism for healthyPatient
  // as other items.

  const healthyPatientItem = macro.healthyPatient ? (0, _macro.macroToItemArrayBase)({
    healthyPatient: macro.healthyPatient
  }, '', _macro.macroLabelMap)[0] : null;
  const sections = [];

  if (items.length > 0 || healthyPatientItem) {
    sections.push((0, _preact.h)("div", null, (0, _preact.h)(Title, null, "General:"), (0, _preact.h)("div", {
      className: "ml1"
    }, items.map(item => (0, _preact.h)(ItemEntry, {
      key: item.path,
      item: item
    })), healthyPatientItem && (0, _preact.h)(ItemEntry, {
      key: "healthyPatient",
      item: healthyPatientItem
    }))));
  }

  if (macro.airways) {
    sections.push((0, _preact.h)("div", null, (0, _preact.h)(Title, null, "Airways:"), macro.airways.map((airway, index) => {
      const {
        type
      } = airway,
            rest = _objectWithoutProperties(airway, ["type"]);

      const basePath = "airways.[".concat(index, "]");
      const array = (0, _macro.macroToItemArrayBase)(rest, basePath, _macro.macroAirwayLabelMap);
      return (0, _preact.h)(CategoryEntry, {
        key: "aw_".concat(index),
        path: basePath,
        name: _airwayTypes.airwayTypeLookup[type].name,
        items: array
      });
    })));
  }

  if (macro.vitalTypes) {
    sections.push((0, _preact.h)("div", null, (0, _preact.h)(Title, null, "Vital Types:"), (0, _preact.h)("div", {
      className: "ml1"
    }, vitalItems.map(item => (0, _preact.h)(ItemEntry, {
      key: item.path,
      item: item
    })))));
  }

  if (macro.localDrugs) {
    sections.push((0, _preact.h)(DrugCategory, {
      title: "Local Anesthetics:",
      drugs: macro.localDrugs,
      baseProp: "local",
      itemsToHide: ['rxcui', 'percent', 'epi']
    }));
  }

  if (macro.parenDrugs) {
    sections.push((0, _preact.h)(DrugCategory, {
      title: "Parenteral Drugs:",
      drugs: macro.parenDrugs,
      baseProp: "parenDrugs",
      itemsToHide: ['unit']
    }));
  }

  if (macro.oralSedatives) {
    sections.push((0, _preact.h)(DrugCategory, {
      title: "Oral Sedatives:",
      drugs: macro.oralSedatives,
      baseProp: "oralSedatives",
      itemsToHide: ['unit']
    }));
  }

  if (macro.infusionDrugs) {
    sections.push((0, _preact.h)(DrugCategory, {
      title: "Infusion Drugs:",
      drugs: macro.infusionDrugs,
      baseProp: "infusionDrugs",
      itemsToHide: []
    }));
  }

  return (0, _preact.h)("div", rest, sections.map((section, index) => {
    const isLast = sections.length - 1 === index;
    return (0, _preact.h)("div", {
      key: index,
      className: isLast ? null : 'mb2'
    }, section);
  }));
};

exports.MacroDescription = MacroDescription;