"use strict";

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

var _preact = require("preact");

var _2 = require("../");

var _section = _interopRequireDefault(require("./section"));

var _componentCode = _interopRequireDefault(require("./component-code"));

var _question = require("../utils/question");

var _smartSuggestionUtils = require("../utils/smart-suggestion-utils");

var _button = require("../button");

var _birthDateField = require("../form-fields/birth-date-field");

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

var _milliseconds = _interopRequireDefault(require("milliseconds"));

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

const timeIncMin = Date.now() - _milliseconds.default.days(3);

const timeIncMax = Date.now() + _milliseconds.default.hours(50);

const fields = [{
  name: 'text',
  label: 'This is the "label"',
  subLabel: 'this is the subLabel',
  onInput: true
}, {
  name: 'textDisabled',
  label: 'This is a disabled input',
  subLabel: 'this is the subLabel',
  onInput: true,
  disabled: true
}, {
  name: 'textWithSuggestions',
  label: 'This one has smart suggestions',
  subLabel: 'this is the subLabel',
  autoSuggestType: 'single',
  onInput: true
}, {
  name: 'medications',
  label: 'Multi-medication',
  type: 'medications'
}, {
  name: 'medication',
  label: 'Single Medication',
  type: 'medication'
}, {
  name: 'signature',
  label: 'Signature',
  type: 'signature',
  // confirmButtonText: 'Boom!',
  signatureLineName: 'Someone'
}, _birthDateField.birthDateField, {
  name: 'multiColor',
  label: 'Multiple select',
  type: 'select',
  multiple: true,
  options: [{
    label: 'pick a color'
  }],
  optGroups: [{
    label: 'Primary',
    options: ['Red', 'Blue', 'Green', 'Yellow']
  }, {
    label: 'Other',
    options: ['Purple', 'Chartreuse', 'Baby Blue']
  }]
}, {
  name: 'stuffList',
  label: 'Stuff List',
  subLabel: 'Requires at least 3 items.',
  type: 'listTextarea',
  test: val => !val || val.length < 3
}, {
  name: 'color',
  label: 'Regular select',
  type: 'select',
  options: [{
    label: 'pick a color',
    value: null
  }, {
    label: 'Red',
    value: 'red'
  }, {
    label: 'Blue',
    value: 'blue'
  }]
}, {
  label: 'Incrementor Field',
  name: 'incrementor',
  type: 'incrementor',
  fieldSpecificProps: {
    showStep: false,
    width: '40px',
    min: 0,
    max: 55
  },
  test: val => val < 1 ? 'value has to be more than zero' : ''
}, {
  label: 'Incrementor Field',
  subLabel: 'with decimals',
  name: 'decimalIncrementor',
  type: 'incrementor',
  fieldSpecificProps: {
    showStep: true,
    width: '250px',
    min: 1,
    max: 3,
    step: 0.1
  },
  test: val => val < 1 ? 'value has to be more than zero' : ''
}, {
  label: 'Time Incrementor Field',
  name: 'timeIncrementor',
  type: 'timeIncrementor',
  fieldSpecificProps: {
    min: timeIncMin,
    max: timeIncMax
  },
  test: val => val < 1 ? 'value has to be more than zero' : ''
}, {
  label: 'Secured By',
  name: 'securedWith',
  type: 'choices',
  choices: [(0, _question.choice)('Tape'), (0, _question.choice)('Tegaderm'), {
    val: 'hi',
    name: 'Something Disabled',
    disabled: true
  }],
  showOther: true,
  otherInitText: 'something else'
}, {
  label: 'Reason for sedation',
  name: 'reasonForSedation',
  type: 'multiButton',
  choices: [(0, _question.choice)('Acute Situational Anxiety'), (0, _question.choice)('Patient choice for comfort'), (0, _question.choice)('Non-cooperative behavior'), (0, _question.choice)('Post Traumatic Stress Disorder'), (0, _question.choice)('Inability to achieve profound local anesthesia')],
  showOther: true,
  subLabel: 'You can select more than one',
  tourVal: 'Inability to achieve profound local anesthesia'
}, {
  label: 'Decimal field',
  name: 'decimal',
  type: 'decimal',
  maxLenBefore: 3,
  maxLenAfter: 3,
  subLabel: 'only allows 3 numbers before or after the decimal'
}, ..._2.heightFieldDefs, ..._2.weightFieldDefs, {
  label: 'A boolean checkbox',
  name: 'check',
  type: 'checkbox',
  fieldSpecificProps: {
    checkboxLabel: 'Something different',
    valueIfTrue: 'test'
  },
  subLabel: 'only allows 3 numbers before or after the decimal'
}, {
  label: 'Infusion Unit Field',
  name: 'infusionUnit',
  type: 'infusionUnit'
}, {
  label: 'Infusion Bolus Unit Field',
  options: [{
    label: 'mcg',
    value: 'mcg'
  }],
  name: 'infusionBolusUnit',
  type: 'infusionBolusUnit'
}];

class FormsSection extends _preact.Component {
  constructor(props) {
    super(props);
    this.state = {
      incrementor: 1,
      decimalIncrementor: 1,
      timeIncrementor: Date.now(),
      color: 'blue',
      multiColor: 'Red,Green',
      stuffList: ['first', 'second']
    };
    fields.forEach(field => {
      if (!this.state.hasOwnProperty(field.name)) {
        this.state[field.name] = '';

        if (field.disabled) {
          this.state[field.name] = 'disabled value';
        }
      }
    });
  }

  updateValue(updates) {
    this.setState(updates, () => {});
  }

  render(_, state) {
    const processedFields = (0, _smartSuggestionUtils.buildQuestionList)(fields, this.state, {
      textWithSuggestions: {
        suggestions: [{
          score: 2,
          phrase: 'Jsjsj'
        }],
        deleting: false
      }
    });
    return (0, _preact.h)(_section.default, {
      title: "Forms"
    }, (0, _preact.h)(_componentCode.default, {
      name: "Form",
      code: "<Form\n  autocomplete=\"off\"\n  onSubmit={() => {\n    !state.missingItems && alert('finished')\n  }}\n  updateValue={updates => this.updateValue(updates)}\n  removeSuggestion={(fieldName, suggestion) =>\n    alert(`removeSuggestion(\"${fieldName}\", \"${suggestion}\")`)\n  }\n  fields={processedFields}\n  bottom={\n    <div>\n      <Button>Submit</Button>\n    </div>\n  }\n/>"
    }, (0, _preact.h)(_2.Form, {
      autocomplete: "off",
      onSubmit: () => {
        const finalValues = processedFields.reduce((res, item) => {
          res[item.name] = item.finalValue;
          return res;
        }, {});
        alert('submit called:' + JSON.stringify(finalValues, null, 2));
      },
      updateValue: updates => this.updateValue(updates),
      removeSuggestion: (fieldName, suggestion) => alert("removeSuggestion(\"".concat(fieldName, "\", \"").concat(suggestion, "\")")),
      otherFieldTypes: {
        infusionUnit: _formFields.InfusionUnitField,
        infusionBolusUnit: _formFields.InfusionBolusField
      },
      fields: processedFields,
      bottom: (0, _preact.h)("div", {
        className: "mv3"
      }, (0, _preact.h)(_button.Button, {
        primary: true,
        large: true,
        type: "submit"
      }, "Submit"))
    }), (0, _preact.h)("div", {
      className: "f7"
    }, "Form Definition:", (0, _preact.h)("pre", null, (0, _preact.h)("code", {
      className: "f7"
    }, JSON.stringify(processedFields, null, 2))), "Form state:", (0, _preact.h)("pre", null, (0, _preact.h)("code", {
      className: "f7"
    }, JSON.stringify(state, null, 2))))));
  }

}

exports.FormsSection = FormsSection;