"use strict";

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

var _preact = require("preact");

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

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

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

class MedicationEntryField extends _preact.Component {
  focusOnInput() {
    const input = this.base.querySelector('input');

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

  render({
    item,
    updateValue,
    className
  }) {
    const drugName = item.value && item.value.name;
    return (0, _preact.h)("div", {
      className: className
    }, (0, _preact.h)(_formFields.Label, {
      item: item,
      updateValue: updateValue
    }), !drugName && (0, _preact.h)(_drugAutoCompleteInput.DrugAutoCompleteInput, {
      item: item,
      selectCallback: updateValue
    }), drugName && (0, _preact.h)(_selectedItem.SelectedItem, {
      key: drugName,
      label: drugName,
      onClose: e => {
        e.preventDefault();
        updateValue('');
        setTimeout(() => this.focusOnInput(), 200);
      }
    }));
  }

}

exports.MedicationEntryField = MedicationEntryField;