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 from 'react';
import PropTypes from 'prop-types';
import { Snackbar, Button } from '@material-ui/core';
import { style } from 'typestyle';
import fixDangerousChange from '../../utils/fixDangerousChange';
var cn = {
  button: style({
    marginRight: 8
  })
};
var stopFixing = false;

var FixAndMessage =
/*#__PURE__*/
function (_React$Component) {
  _inherits(FixAndMessage, _React$Component);

  function FixAndMessage() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, FixAndMessage);

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

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleBeforeChange", function (cm, change) {
      if (stopFixing) return;
      var localization = _this.props.localization;
      var removed = cm.doc.getRange(change.from, change.to);
      var result = fixDangerousChange(change, removed);

      if (result.hankaku) {
        _this.setState({
          fixedMessage: localization.editorCard.hankaku
        });
      } else if (result.canceled) {
        _this.setState({
          fixedMessage: localization.editorCard.canceled
        });
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleStopFixing", function () {
      if (stopFixing) return;
      stopFixing = true;

      _this.setState({
        fixedMessage: ''
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleMessageClose", function (event, reason) {
      if (reason === 'clickaway') {
        return;
      }

      _this.setState({
        fixedMessage: ''
      });
    });

    return _this;
  }

  _createClass(FixAndMessage, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (this.props.codemirror !== prevProps.codemirror) {
        if (prevProps.codemirror) {
          prevProps.codemirror.off('beforeChange', this.handleBeforeChange);
        }

        if (this.props.codemirror) {
          this.props.codemirror.on('beforeChange', this.handleBeforeChange);
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      var localization = this.props.localization;
      return React.createElement(Snackbar, {
        anchorOrigin: {
          vertical: 'bottom',
          horizontal: 'center'
        },
        open: Boolean(this.state.fixedMessage),
        message: this.state.fixedMessage,
        autoHideDuration: 600000,
        onClose: this.handleMessageClose,
        action: React.createElement(React.Fragment, null, React.createElement(Button, {
          color: "primary",
          variant: "contained",
          className: cn.button,
          onClick: this.handleMessageClose
        }, localization.editorCard.ok), React.createElement(Button, {
          color: "primary",
          className: cn.button,
          onClick: this.handleStopFixing
        }, localization.editorCard.stopAction))
      });
    }
  }]);

  return FixAndMessage;
}(React.Component);

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

export { FixAndMessage as default };