"use strict";

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

var _preact = require("preact");

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

var _inputs = require("./inputs");

var _processListText = require("./utils/process-list-text");

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; }

class ListTextareaField extends _preact.Component {
  constructor(props) {
    super(props);
    this.state = {
      originalData: null,
      data: null,
      itemsToSave: null,
      itemsToSaveCount: null
    };

    if (Array.isArray(props.item.value)) {
      this.receiveList(props.item.value);
    }

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

  receiveList(list) {
    const asString = list.join('\n');
    this.setState({
      originalData: asString,
      data: asString,
      length: list.length,
      itemsToSave: null,
      itemsToSaveCount: null
    });
  }

  onInput(e) {
    const {
      saveableRows,
      cleanedText,
      totalRowCount,
      saveableItemCount
    } = (0, _processListText.processListText)(e.target.value || '');
    this.setState({
      length: totalRowCount,
      data: cleanedText,
      itemsToSave: saveableRows,
      itemsToSaveCount: saveableItemCount
    });
    this.props.updateValue && this.props.updateValue(saveableRows);
  }

  render() {
    // eslint-disable-next-line no-unused-vars
    const _this$props = this.props,
          {
      item,
      updateValue
    } = _this$props,
          rest = _objectWithoutProperties(_this$props, ["item", "updateValue"]);

    const {
      data,
      length,
      itemsToSaveCount
    } = this.state;
    return (0, _preact.h)("div", rest, item.label && (0, _preact.h)(_formFields.Label, {
      item: item
    }), (0, _preact.h)(_inputs.Textarea, {
      "data-e2e": "".concat(item.name, "Textarea"),
      onInput: this.onInput,
      rows: Math.max(length || 0, 4),
      className: "w-100",
      value: data
    }), itemsToSaveCount != null && (0, _preact.h)("p", {
      className: "mt1",
      "data-e2e": "".concat(item.name, "Desc")
    }, "Contains ", itemsToSaveCount, " items"));
  }

}

exports.ListTextareaField = ListTextareaField;