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

import { Component } from 'react';
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';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * 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() {
    super(...arguments);
    _defineProperty(this, "state", {
      editing: false,
      draft: ''
    });
    _defineProperty(this, "handleEdit", () => {
      this.setState(_ref => {
        let {
          editing
        } = _ref;
        return {
          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", () => {
      if (this.props.onChange) {
        this.props.onChange(this.state.draft);
      }
      this.handleEdit();
    });
  }
  render() {
    const {
      draft,
      editing
    } = this.state;
    const {
      baseClassName,
      className,
      onChange,
      value,
      ...props
    } = this.props;
    return /*#__PURE__*/_jsxs("div", {
      className: classNames(baseClassName, className),
      ...props,
      children: [editing ? /*#__PURE__*/_jsxs("div", {
        className: `${baseClassName}__edit`,
        children: [/*#__PURE__*/_jsx(Input, {
          className: `${baseClassName}__edit-input`,
          value: draft,
          onChange: this.handleChange,
          onKeyDown: this.handleEditKey,
          autoFocus: true
        }), /*#__PURE__*/_jsxs("div", {
          className: `${baseClassName}__edit-actions`,
          children: [/*#__PURE__*/_jsx(Tooltip, {
            title: /*#__PURE__*/_jsx(Translate, {
              content: "InPlaceEdit.okButtonHint",
              fallback: locale.okButtonHint
            }),
            children: /*#__PURE__*/_jsx(Button, {
              ghost: true,
              className: classNames(`${baseClassName}__control-button`, `${baseClassName}__control-button--ok`),
              icon: "check-mark",
              onClick: this.handleEditOk
            })
          }), /*#__PURE__*/_jsx(Tooltip, {
            title: /*#__PURE__*/_jsx(Translate, {
              content: "InPlaceEdit.cancelButtonHint",
              fallback: locale.cancelButtonHint
            }),
            children: /*#__PURE__*/_jsx(Button, {
              ghost: true,
              className: classNames(`${baseClassName}__control-button`, `${baseClassName}__control-button--cancel`),
              icon: "cross-mark",
              onClick: this.handleEdit
            })
          })]
        })]
      }) : value, onChange && !editing && /*#__PURE__*/_jsx(Tooltip, {
        title: /*#__PURE__*/_jsx(Translate, {
          content: "InPlaceEdit.editButtonHint",
          fallback: locale.editButtonHint
        }),
        children: /*#__PURE__*/_jsx(Button, {
          ghost: true,
          className: classNames(`${baseClassName}__control-button`, `${baseClassName}__control-button--edit`),
          onClick: this.handleEdit,
          icon: {
            name: 'pencil',
            className: `${baseClassName}__control-button-icon`
          }
        })
      })]
    });
  }
}
_defineProperty(InPlaceEdit, "defaultProps", {
  value: null,
  onChange: null,
  className: null,
  baseClassName: `${CLS_PREFIX}in-place-edit`
});
export default InPlaceEdit;