"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.MathInput = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _styles = require("@material-ui/core/styles");

var _classnames = _interopRequireDefault(require("classnames"));

var mq = _interopRequireWildcard(require("./mq"));

var _keys = require("./keys");

var _keypad = _interopRequireDefault(require("./keypad"));

var _debug = _interopRequireDefault(require("debug"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var log = (0, _debug["default"])('pie-lib:math-input');

var MathInput = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(MathInput, _React$Component);

  var _super = _createSuper(MathInput);

  function MathInput(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, MathInput);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "keypadPress", function (key) {
      log('[keypadPress] key:', key);

      if (!_this.input) {
        return;
      }

      if (key.latex && !key.command) {
        _this.input.write(key.latex);
      } else if (key.write) {
        _this.input.write(key.write);
      } else if (key.command) {
        _this.input.command(key.command);
      } else if (key.keystroke) {
        _this.input.keystroke(key.keystroke);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "inputFocus", function () {
      _this.setState({
        focused: true
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "inputBlur", function () {
      _this.setState({
        focused: false
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "changeLatex", function (l) {
      var onChange = _this.props.onChange;

      if (onChange && l !== _this.props.latex) {
        log('[changeLatex]', l, _this.props.latex);
        onChange(l);
      }
    });
    _this.state = {
      focused: false
    };
    return _this;
  }

  (0, _createClass2["default"])(MathInput, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          classes = _this$props.classes,
          className = _this$props.className,
          keyset = _this$props.keyset,
          latex = _this$props.latex;
      var focused = this.state.focused;
      var showKeypad = true; // TODO: add support for different displayModes - displayMode === 'block' || focused;

      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(classes.mathInput, className, focused && classes.focused)
      }, /*#__PURE__*/_react["default"].createElement(mq.Input, {
        className: classes.mqInput,
        innerRef: function innerRef(r) {
          return _this2.input = r;
        },
        onFocus: this.inputFocus,
        onBlur: this.inputBlur,
        latex: latex,
        onChange: this.changeLatex
      }), showKeypad && /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.pad
      }, /*#__PURE__*/_react["default"].createElement(_keypad["default"], {
        baseSet: _keys.baseSet,
        additionalKeys: keyset,
        onPress: this.keypadPress
      })));
    }
  }]);
  return MathInput;
}(_react["default"].Component);

exports.MathInput = MathInput;
(0, _defineProperty2["default"])(MathInput, "propTypes", {
  classes: _propTypes["default"].object.isRequired,
  className: _propTypes["default"].string,
  keyset: _propTypes["default"].array,
  displayMode: _propTypes["default"].oneOf(['block', 'block-on-focus']),
  latex: _propTypes["default"].string,
  onChange: _propTypes["default"].func
});
(0, _defineProperty2["default"])(MathInput, "defaultProps", {
  keyset: [],
  displayMode: 'block'
});
var grey = 'rgba(0, 0, 0, 0.23)';

var styles = function styles(theme) {
  return {
    formGroup: {
      display: 'flex',
      textAlign: 'right',
      "float": 'right'
    },
    pad: {
      width: '100%',
      display: 'flex'
    },
    mathInput: {
      borderRadius: '4px',
      border: "solid 1px ".concat(grey),
      marginTop: theme.spacing.unit,
      marginBottom: theme.spacing.unit,
      transition: 'border 200ms linear'
    },
    focused: {
      border: "solid 1px ".concat(theme.palette.primary.main)
    },
    mqInput: {
      width: '100%',
      border: "solid 0px ".concat(theme.palette.primary.light),
      transition: 'border 200ms linear',
      padding: theme.spacing.unit,
      '&.mq-focused': {
        outline: 'none',
        boxShadow: 'none',
        border: "solid 0px ".concat(theme.palette.primary.dark)
      }
    }
  };
};

var _default = (0, _styles.withStyles)(styles)(MathInput);

exports["default"] = _default;
//# sourceMappingURL=math-input.js.map