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, { Component } from 'react';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import TextField from '@material-ui/core/TextField';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
var MimeTypes = {
  'text/javascript': '.js',
  'text/x-markdown': '.md',
  'application/json': '.json',
  'text/html': '.html',
  'text/css': '.css',
  'text/plain': '',
  'text/x-glsl': '.sort'
};

var getUniqueId = function (i) {
  return function () {
    return ++i;
  };
}(0);

var cn = {
  dropDown: style({
    height: 43
  })
};

var FilenameInput =
/*#__PURE__*/
function (_Component) {
  _inherits(FilenameInput, _Component);

  function FilenameInput(props) {
    var _this;

    _classCallCheck(this, FilenameInput);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(FilenameInput).call(this, props));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      name: _this.props.defaultName || 'filename',
      type: _this.props.defaultType || 'text/javascript'
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleNameChange", function (event) {
      _this.setState({
        name: event.target.value
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTypeChange", function (event) {
      _this.setState({
        type: event.target.value
      });
    });

    _this.id = 'FILENAME_INPUT_' + getUniqueId();
    return _this;
  }

  _createClass(FilenameInput, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      if (this.input) {
        this.timer = window.setTimeout(function () {
          _this2.input.focus();

          _this2.input.select();
        }, 100);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.timer) {
        window.clearTimeout(this.timer);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$state = this.state,
          name = _this$state.name,
          type = _this$state.type;
      var disabled = this.props.disabled;
      var style = this.props.style;
      return React.createElement("div", {
        style: style
      }, React.createElement(TextField, {
        id: this.id,
        ref: function ref(textField) {
          return _this3.input = textField && textField.input;
        }
      }, React.createElement("input", {
        autoFocus: true,
        defaultValue: name,
        disabled: disabled,
        onChange: this.handleNameChange
      })), React.createElement(Select, {
        value: type,
        disabled: disabled,
        onChange: this.handleTypeChange,
        className: cn.dropDown
      }, Object.keys(MimeTypes).map(function (type) {
        return React.createElement(MenuItem, {
          key: type,
          value: type
        }, MimeTypes[type]);
      })));
    }
  }, {
    key: "value",
    get: function get() {
      var _this$state2 = this.state,
          name = _this$state2.name,
          type = _this$state2.type;
      return name + MimeTypes[type];
    }
  }, {
    key: "name",
    get: function get() {
      return this.state.name;
    }
  }, {
    key: "type",
    get: function get() {
      return this.state.type;
    }
  }]);

  return FilenameInput;
}(Component);

_defineProperty(FilenameInput, "propTypes", {
  defaultName: PropTypes.string,
  defaultType: PropTypes.string,
  disabled: PropTypes.bool,
  style: PropTypes.object
});

export { FilenameInput as default };