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";
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import TextField from '@material-ui/core/TextField';
import Typography from '@material-ui/core/Typography';
import { separate } from '../../File/';
var cn = {
  root: style({
    display: 'flex',
    justifyContent: 'flex-start',
    alignItems: 'baseline'
  }),
  textField: style({
    width: 'auto',
    flex: '0 1 auto',
    height: 40
  })
};

var Filename =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(Filename, _PureComponent);

  function Filename() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Filename);

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      isEditing: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleInput", function (ref) {
      var _this$props = _this.props,
          file = _this$props.file,
          onChange = _this$props.onChange;
      if (!ref) return;

      ref.input.onchange = function (_ref) {
        var target = _ref.target;
        onChange(file, target.value);

        _this.setState({
          isEditing: false
        });
      };

      ref.input.onblur = function () {
        _this.setState({
          isEditing: false
        });
      };
    });

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleDoubleTap", function (event) {
      event.stopPropagation();

      if (_this.touchFlag) {
        _this.setState({
          isEditing: true
        });

        return;
      }

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTextFieldTap", function (event) {
      event.stopPropagation();
    });

    return _this;
  }

  _createClass(Filename, [{
    key: "render",
    value: function render() {
      var file = this.props.file;
      var isEditing = this.state.isEditing;

      var _separate = separate(file.name),
          path = _separate.path,
          plain = _separate.plain,
          ext = _separate.ext,
          name = _separate.name;

      return React.createElement("div", {
        className: cn.root
      }, React.createElement(Typography, {
        color: "textSecondary"
      }, path), isEditing ? React.createElement(TextField, {
        id: name,
        defaultValue: plain,
        ref: this.handleInput,
        className: cn.textField,
        onClick: this.handleTextFieldTap
      }) : React.createElement(Typography, {
        color: "textPrimary",
        onClick: this.handleDoubleTap
      }, plain), React.createElement(Typography, {
        color: "textSecondary",
        variant: "caption"
      }, ext));
    }
  }]);

  return Filename;
}(PureComponent);

_defineProperty(Filename, "propTypes", {
  file: PropTypes.object.isRequired,
  onChange: PropTypes.func.isRequired
});

export { Filename as default };