"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Tokenizer = 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 _controls = _interopRequireDefault(require("./controls"));

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

var _builder = require("./builder");

var _clone = _interopRequireDefault(require("lodash/clone"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var _differenceWith = _interopRequireDefault(require("lodash/differenceWith"));

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

var _styleUtils = require("../../style-utils");

var _tokenText = _interopRequireDefault(require("./token-text"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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 Tokenizer = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Tokenizer, _React$Component);

  var _super = _createSuper(Tokenizer);

  function Tokenizer(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Tokenizer);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChangeHandler", function (token, mode) {
      _this.props.onChange(token, mode);

      _this.setState({
        mode: mode
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "toggleCorrectMode", function () {
      return _this.setState({
        setCorrectMode: !_this.state.setCorrectMode
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "clear", function () {
      _this.onChangeHandler([], '');
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "buildTokens", function (type, fn) {
      var text = _this.props.text;
      var tokens = fn(text);

      _this.onChangeHandler(tokens, type);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "selectToken", function (newToken, tokensToRemove) {
      var tokens = _this.props.tokens;
      var update = (0, _differenceWith["default"])((0, _clone["default"])(tokens), tokensToRemove, _isEqual["default"]);
      update.push(newToken);

      _this.onChangeHandler(update, _this.state.mode);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "tokenClick", function (token) {
      var setCorrectMode = _this.state.setCorrectMode;

      if (setCorrectMode) {
        _this.setCorrect(token);
      } else {
        _this.removeToken(token);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "tokenIndex", function (token) {
      var tokens = _this.props.tokens;
      return tokens.findIndex(function (t) {
        return t.text == token.text && t.start == token.start && t.end == token.end;
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setCorrect", function (token) {
      var tokens = _this.props.tokens;

      var index = _this.tokenIndex(token);

      if (index !== -1) {
        var t = tokens[index];
        t.correct = !t.correct;
        var update = (0, _clone["default"])(tokens);
        update.splice(index, 1, t);

        _this.onChangeHandler(update, _this.state.mode);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "removeToken", function (token) {
      var tokens = _this.props.tokens;

      var index = _this.tokenIndex(token);

      if (index !== -1) {
        var update = (0, _clone["default"])(tokens);
        update.splice(index, 1);

        _this.onChangeHandler(update, _this.state.mode);
      }
    });
    _this.state = {
      setCorrectMode: false,
      mode: ''
    };
    return _this;
  }

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

      var _this$props = this.props,
          text = _this$props.text,
          tokens = _this$props.tokens,
          classes = _this$props.classes,
          className = _this$props.className;
      var setCorrectMode = this.state.setCorrectMode;
      var tokenClassName = (0, _classnames["default"])(classes.text, setCorrectMode && classes.noselect);
      var rootName = (0, _classnames["default"])(classes.tokenizer, className);
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: rootName
      }, /*#__PURE__*/_react["default"].createElement(_controls["default"], {
        onClear: this.clear,
        onWords: function onWords() {
          return _this2.buildTokens('words', _builder.words);
        },
        onSentences: function onSentences() {
          return _this2.buildTokens('sentence', _builder.sentences);
        },
        onParagraphs: function onParagraphs() {
          return _this2.buildTokens('paragraphs', _builder.paragraphs);
        },
        setCorrectMode: setCorrectMode,
        onToggleCorrectMode: this.toggleCorrectMode
      }), /*#__PURE__*/_react["default"].createElement(_tokenText["default"], {
        className: tokenClassName,
        text: text,
        tokens: tokens,
        onTokenClick: this.tokenClick,
        onSelectToken: this.selectToken
      }));
    }
  }]);
  return Tokenizer;
}(_react["default"].Component);

exports.Tokenizer = Tokenizer;
(0, _defineProperty2["default"])(Tokenizer, "propTypes", {
  text: _propTypes["default"].string.isRequired,
  tokens: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    text: _propTypes["default"].string,
    correct: _propTypes["default"].bool,
    start: _propTypes["default"].number,
    end: _propTypes["default"].number
  })),
  classes: _propTypes["default"].object.isRequired,
  className: _propTypes["default"].string,
  onChange: _propTypes["default"].func.isRequired
});
(0, _defineProperty2["default"])(Tokenizer, "defaultProps", {});

var _default = (0, _styles.withStyles)(function () {
  return {
    text: {
      whiteSpace: 'pre-wrap'
    },
    noselect: _objectSpread({}, (0, _styleUtils.noSelect)())
  };
})(Tokenizer);

exports["default"] = _default;
//# sourceMappingURL=index.js.map