"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _message = require("../message");
var _editableTextControlled = _interopRequireDefault(require("./editable-text-controlled"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function isEscapeKey(key) {
  return key === 'Escape';
}
/** Text field with an edit */
class EditableText extends _react.default.Component {
  static defaultProps = {
    onSubmit: () => {},
    options: {},
    cancelEditsOnEscape: false,
    shouldDisplayControls: true,
    areHyperLinksClickable: false
  };
  constructor(props) {
    super(props);
    this.startEditing = this.startEditing.bind(this);
    this.submitEdit = this.submitEdit.bind(this);
    this.onChange = this.onChange.bind(this);
    this.handleCancel = this.handleCancel.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.onKeyDown = this.onKeyDown.bind(this);
    this.state = {
      isEditing: false,
      value: props.value ?? ''
    };
    this.originalValue = '';
    this.editInputRef = /*#__PURE__*/_react.default.createRef();
  }
  componentDidUpdate(prevProps) {
    if (prevProps.value !== this.props.value && this.props.value !== undefined) {
      this.setState({
        value: this.props.value
      });
    }
  }
  startEditing() {
    this.originalValue = this.state.value;
    this.setState({
      isEditing: true
    });
  }
  async submitEdit() {
    this.setState({
      isEditing: false
    });
    try {
      await this.props.onSubmit?.(this.state.value);
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (e) {
      this.setState({
        value: this.originalValue
      });
      this.originalValue = '';

      // Propagate false to inputs and form
      return false;
    }
  }
  onChange(e) {
    this.setState({
      value: e.target.value
    });
    if (typeof this.props.onChange === 'function') {
      this.props.onChange(e.target.value);
    }
  }
  onKeyDown(e) {
    if (this.props.cancelEditsOnEscape && isEscapeKey(e.key)) {
      this.setState({
        isEditing: false,
        value: this.originalValue
      });
    }
  }
  handleCancel() {
    this.setState({
      isEditing: false,
      value: this.originalValue
    });
  }
  handleSubmit() {
    if ([this.props?.options?.validity?.getType?.(this.state.value), this.props?.options?.validity?.type].includes(_message.MESSAGE_TYPES.ERROR)) {
      this.setState({
        isEditing: false,
        value: this.originalValue
      });
    } else {
      this.submitEdit();
    }
  }
  render() {
    const {
      isEditing,
      value
    } = this.state;
    const {
      inputLabel,
      isMultiline,
      isDisabled,
      isLoading,
      buttonLabel,
      placeholder,
      testId,
      options,
      inputClassName,
      textClassName,
      shouldDisplayControls,
      areHyperLinksClickable,
      buttonTooltip,
      editButtonTestId,
      buttonAriaLabel
    } = this.props;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_editableTextControlled.default, {
      isEditing: isEditing,
      onSubmit: this.handleSubmit,
      onBlur: this.handleSubmit,
      onCancel: this.handleCancel,
      onKeyDown: this.onKeyDown,
      inputLabel: inputLabel,
      isMultiline: isMultiline,
      placeholder: placeholder,
      onChange: this.onChange,
      value: value,
      editInputRef: this.editInputRef,
      options: options,
      inputClassName: inputClassName,
      textClassName: textClassName,
      startEditing: this.startEditing,
      buttonLabel: buttonLabel,
      isDisabled: isDisabled,
      isLoading: isLoading,
      shouldDisplayControls: shouldDisplayControls,
      areHyperLinksClickable: areHyperLinksClickable,
      testId: testId,
      buttonTooltip: buttonTooltip,
      editButtonTestId: editButtonTestId,
      buttonAriaLabel: buttonAriaLabel
    });
  }
}
exports.default = EditableText;