import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
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 Dialog from '@material-ui/core/Dialog';
import DialogTitle from '@material-ui/core/DialogTitle';
import DialogContent from '@material-ui/core/DialogContent';
import DialogActions from '@material-ui/core/DialogActions';
import TextField from '@material-ui/core/TextField';
import CodeMirror from 'codemirror';
import 'codemirror/mode/meta';
import ImmutableFile from '../File/ImmutableFile';
import { Button } from '@material-ui/core';
import { style } from 'typestyle';
import withFiles from '../store/withFiles';

var getSeed = function getSeed(type) {
  if (type === 'application/json') {
    return '{}';
  }

  return '\n'.repeat(30);
};

var cn = {
  button: style({
    marginRight: 20
  })
};

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

  function AddDialog() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, AddDialog);

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      name: ''
    });

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

    return _this;
  }

  _createClass(AddDialog, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          localization = _this$props.localization,
          props = _objectWithoutProperties(_this$props, ["localization"]);

      return React.createElement(Dialog, props, React.createElement(DialogTitle, null, localization.addDialog.title), React.createElement(DialogContent, null, React.createElement(TextField, {
        autoFocus: true,
        fullWidth: true,
        value: this.state.name,
        label: localization.addDialog.fileName,
        placeholder: "main.js",
        onChange: this.handleUpdateName
      })), React.createElement(DialogActions, null, React.createElement(Button, {
        variant: "outlined",
        onClick: props.onClose
      }, localization.addDialog.cancel), React.createElement(AddButton, {
        name: this.state.name,
        onClick: props.onClose
      }, this.props.localization.addDialog.add)));
    }
  }]);

  return AddDialog;
}(Component);

_defineProperty(AddDialog, "propTypes", {
  localization: PropTypes.object.isRequired
});

_defineProperty(AddDialog, "defaultProps", {
  onClose: function onClose() {}
});

export { AddDialog as default };
var AddButton = withFiles({
  watch: ''
})(function (props) {
  var onClick = function onClick() {
    var _ref = CodeMirror.findModeByFileName(props.name) || {
      mime: 'text/plain'
    },
        mime = _ref.mime;

    var file = new ImmutableFile({
      name: props.name,
      type: mime,
      text: getSeed(mime)
    });
    props.putFile(file);
    props.onClick && props.onClick();
  };

  return React.createElement(Button, {
    variant: "contained",
    color: "primary",
    className: cn.button,
    onClick: onClick
  }, props.children);
});