import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/* eslint-disable react/no-deprecated */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Button from '../Button';
import Input from '../Input';
import Tooltip from '../Tooltip';
import Translate from '../Translate';
import locale from './locale/en-US';

/**
 * This component allows users to edit a short text "in place" — in other words, straight in the interface, without
 * opening additional dialog windows. If a text is large enough that it doesn't fit on a single line, you should
 * give users a different way of editing the text.
 * @since 1.8.3
 */
class InPlaceEdit extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      editing: false,
      draft: ''
    });

    _defineProperty(this, "handleEdit", () => {
      this.setState(({
        editing
      }) => ({
        editing: !editing,
        draft: editing ? '' : this.props.value
      }));
    });

    _defineProperty(this, "handleEditKey", event => {
      if (event.key === 'Escape') {
        this.handleEdit();
      } else if (event.key === 'Enter') {
        this.handleEditOk();
      }
    });

    _defineProperty(this, "handleChange", event => {
      this.setState({
        draft: event.target.value
      });
    });

    _defineProperty(this, "handleEditOk", () => {
      this.props.onChange(this.state.draft);
      this.handleEdit();
    });
  }

  render() {
    const {
      draft,
      editing
    } = this.state;
    const {
      baseClassName,
      className,
      onChange,
      value,
      ...props
    } = this.props;
    return /*#__PURE__*/React.createElement("div", _extends({
      className: classNames(baseClassName, className)
    }, props), editing ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(baseClassName, "__edit")
    }, /*#__PURE__*/React.createElement(Input, {
      className: "".concat(baseClassName, "__edit-input"),
      value: draft,
      onChange: this.handleChange,
      onKeyDown: this.handleEditKey,
      autoFocus: true
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(baseClassName, "__edit-actions")
    }, /*#__PURE__*/React.createElement(Tooltip, {
      title: /*#__PURE__*/React.createElement(Translate, {
        content: "InPlaceEdit.okButtonHint",
        fallback: locale.okButtonHint
      })
    }, /*#__PURE__*/React.createElement(Button, {
      ghost: true,
      className: classNames("".concat(baseClassName, "__control-button"), "".concat(baseClassName, "__control-button--ok")),
      icon: "check-mark",
      onClick: this.handleEditOk
    })), /*#__PURE__*/React.createElement(Tooltip, {
      title: /*#__PURE__*/React.createElement(Translate, {
        content: "InPlaceEdit.cancelButtonHint",
        fallback: locale.cancelButtonHint
      })
    }, /*#__PURE__*/React.createElement(Button, {
      ghost: true,
      className: classNames("".concat(baseClassName, "__control-button"), "".concat(baseClassName, "__control-button--cancel")),
      icon: "cross-mark",
      onClick: this.handleEdit
    })))) : value, onChange && !editing && /*#__PURE__*/React.createElement(Tooltip, {
      title: /*#__PURE__*/React.createElement(Translate, {
        content: "InPlaceEdit.editButtonHint",
        fallback: locale.editButtonHint
      })
    }, /*#__PURE__*/React.createElement(Button, {
      ghost: true,
      className: classNames("".concat(baseClassName, "__control-button"), "".concat(baseClassName, "__control-button--edit")),
      onClick: this.handleEdit,
      icon: {
        name: 'pencil',
        className: "".concat(baseClassName, "__control-button-icon")
      }
    })));
  }

}

InPlaceEdit.propTypes = {
  /**
   * Value of the component.
   * @since 1.8.3
   */
  value: PropTypes.node,

  /**
   * Event handler of value change with inline editing form.
   * @since 1.8.3
   */
  onChange: PropTypes.func,

  /**
   * @ignore
   */
  className: PropTypes.string,

  /**
   * @ignore
   */
  baseClassName: PropTypes.string
};
InPlaceEdit.defaultProps = {
  value: null,
  onChange: null,
  className: null,
  baseClassName: "".concat(CLS_PREFIX, "in-place-edit")
};
export default InPlaceEdit;