import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

var _dec, _class, _class2, _temp;

import React, { PureComponent } from 'react';
import { withTheme } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import TextField from '@material-ui/core/TextField';
import Typography from '@material-ui/core/Typography';
import ContentCreate from '@material-ui/icons/Create';
var cn = {
  hint: style({
    fontStyle: 'italic'
  })
};

var getCn = function getCn(props) {
  return {
    label: style({
      fontSize: 16,
      color: props.theme.palette.text.secondary,
      borderBottom: "1px dashed ".concat(props.theme.palette.text.secondary)
    })
  };
};

var EditableLabel = (_dec = withTheme(), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(EditableLabel, _PureComponent);

  function EditableLabel() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, EditableLabel);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(EditableLabel)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      isEditing: _this.props.openImmediately
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "touched", false);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouch", function () {
      if (_this.touched) {
        _this.setState({
          isEditing: true
        });
      }

      _this.touched = true;
      setTimeout(function () {
        return _this.touched = false;
      }, 200);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleBlur", function () {
      _this.setState({
        isEditing: false
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleKeyPress", function () {
      if (event.key === 'Enter') {
        _this.setState({
          isEditing: false
        });
      }
    });

    return _this;
  }

  _createClass(EditableLabel, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.openImmediately && this.input) {
        this.input.focus();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      if (!this.input) return;

      if (!prevState.isEditing && this.state.isEditing) {
        this.input.focus();
      }

      if (prevState.isEditing && !this.state.isEditing) {
        this.props.onEditEnd(this.input.value);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var dcn = getCn(this.props);
      var isEditing = this.state.isEditing;
      var _this$props = this.props,
          value = _this$props.value,
          defaultValue = _this$props.defaultValue;
      var labelText = value || defaultValue;
      var props = Object.assign({}, this.props);
      delete props.onEditEnd;
      delete props.tapTwiceQuickly;
      delete props.openImmediately;
      return isEditing ? React.createElement(TextField, _extends({}, props, {
        ref: function ref(_ref) {
          return _ref && (_this2.input = _ref.input);
        },
        onBlur: this.handleBlur,
        onKeyPress: this.handleKeyPress
      })) : labelText ? React.createElement("div", {
        className: dcn.label,
        style: this.props.style,
        onClick: this.handleTouch
      }, labelText) : React.createElement(Typography, {
        variant: "caption",
        className: cn.hint,
        onClick: this.handleTouch
      }, React.createElement(ContentCreate, null), this.props.tapTwiceQuickly);
    }
  }]);

  return EditableLabel;
}(PureComponent), _defineProperty(_class2, "propTypes", Object.assign({
  theme: PropTypes.object.isRequired,
  openImmediately: PropTypes.bool.isRequired,
  tapTwiceQuickly: PropTypes.string.isRequired,
  onEditEnd: PropTypes.func
}, TextField.propTypes)), _defineProperty(_class2, "defaultProps", {
  openImmediately: false,
  tapTwiceQuickly: 'Tap twice quickly'
}), _temp)) || _class);
export { EditableLabel as default };