"use strict";

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

var _preact = require("preact");

var _formFields = require("./form-fields");

var _selectedItem = require("./selected-item");

var _drugAutoCompleteInput = require("./drug-auto-complete-input");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class MultiMedicationEntryField extends _preact.Component {
  constructor(props) {
    super(props);
    this.focusOnInput = this.focusOnInput.bind(this);

    this.focusOnInputDelayed = () => setTimeout(this.focusOnInput, 200);

    this.addDrug = this.addDrug.bind(this);
  }

  isEmptyArray(arr) {
    return Array.isArray(arr) && !arr.length;
  }

  focusOnInput() {
    const input = this.base && this.base.querySelector('input');

    if (input) {
      input.focus();
    }
  }

  addDrug(drug) {
    const {
      item,
      updateValue
    } = this.props;
    const {
      value
    } = item;
    const valueIsNone = value === 'none';
    const drugs = value && !valueIsNone ? value : [];
    const copy = drugs.slice();

    if (!copy.find(drugEntry => drugEntry.id === drug.id || drugEntry.name === drug.name)) {
      copy.push(drug);
    }

    updateValue(copy);
    this.focusOnInput();
  }

  render() {
    const {
      item,
      updateValue
    } = this.props;
    const {
      value
    } = item;
    const valueIsNone = value === 'none';
    const itemCopy = Object.assign({}, item);
    delete itemCopy.subLabel;
    const modifiedItemForLabel = !value ? _objectSpread(_objectSpread({}, item), {}, {
      buttons: ['none']
    }) : item;
    return (0, _preact.h)("div", null, (0, _preact.h)(_formFields.Label, {
      item: modifiedItemForLabel,
      updateValue: updateValue
    }), !valueIsNone && (0, _preact.h)(_drugAutoCompleteInput.DrugAutoCompleteInput, {
      item: modifiedItemForLabel,
      selectCallback: this.addDrug
    }), (0, _preact.h)("div", {
      className: "mb2"
    }, valueIsNone && (0, _preact.h)(_selectedItem.SelectedItem, {
      key: "none",
      label: "none",
      onClose: e => {
        e.preventDefault();
        updateValue(null);
        this.focusOnInputDelayed();
      }
    }), value && !valueIsNone && value.length !== 0 && (0, _preact.h)("div", null, (0, _preact.h)("div", {
      className: "mb2 fw3 f6 pt2"
    }, "Chosen Medications:"), (0, _preact.h)("div", {
      className: "flex flex-wrap gap2"
    }, value.map(drug => (0, _preact.h)(_selectedItem.SelectedItem, {
      key: drug.name,
      label: drug.name,
      onClose: e => {
        e.preventDefault();
        const newVal = value.filter(item => item.id !== drug.id);
        updateValue(this.isEmptyArray(newVal) ? null : newVal);
        this.focusOnInputDelayed();
      }
    }))))));
  }

}

exports.MultiMedicationEntryField = MultiMedicationEntryField;