"use strict";

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

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

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

var _token = require("./token-select/token");

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

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 log = (0, _debug["default"])('@pie-lib:text-select');
/**
 * Built on TokenSelect uses build.normalize to build the token set.
 */

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

  var _super = _createSuper(TextSelect);

  function TextSelect() {
    var _this;

    (0, _classCallCheck2["default"])(this, TextSelect);

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

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "change", function (tokens) {
      var onChange = _this.props.onChange;

      if (!onChange) {
        return;
      }

      var out = tokens.filter(function (t) {
        return t.selected;
      }).map(function (t) {
        return {
          start: t.start,
          end: t.end
        };
      });
      onChange(out);
    });
    return _this;
  }

  (0, _createClass2["default"])(TextSelect, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          text = _this$props.text,
          disabled = _this$props.disabled,
          tokens = _this$props.tokens,
          selectedTokens = _this$props.selectedTokens,
          className = _this$props.className,
          highlightChoices = _this$props.highlightChoices,
          maxNoOfSelections = _this$props.maxNoOfSelections,
          animationsDisabled = _this$props.animationsDisabled;
      var normalized = (0, _builder.normalize)(text, tokens);
      log('normalized: ', normalized);
      var prepped = normalized.map(function (t) {
        var selectedIndex = selectedTokens.findIndex(function (s) {
          return s.start === t.start && s.end === t.end;
        });
        var selected = selectedIndex !== -1;
        var correct = selected ? t.correct : undefined;
        var isMissing = t.isMissing;
        return _objectSpread(_objectSpread({}, t), {}, {
          selectable: !disabled && t.predefined,
          selected: selected,
          correct: correct,
          isMissing: isMissing
        });
      });
      return /*#__PURE__*/_react["default"].createElement(_tokenSelect["default"], {
        highlightChoices: !disabled && highlightChoices,
        className: className,
        tokens: prepped,
        disabled: disabled,
        onChange: this.change,
        maxNoOfSelections: maxNoOfSelections,
        animationsDisabled: animationsDisabled
      });
    }
  }]);
  return TextSelect;
}(_react["default"].Component);

exports["default"] = TextSelect;
(0, _defineProperty2["default"])(TextSelect, "propTypes", {
  onChange: _propTypes["default"].func,
  disabled: _propTypes["default"].bool,
  tokens: _propTypes["default"].arrayOf(_propTypes["default"].shape(_token.TokenTypes)).isRequired,
  selectedTokens: _propTypes["default"].arrayOf(_propTypes["default"].shape(_token.TokenTypes)).isRequired,
  text: _propTypes["default"].string.isRequired,
  className: _propTypes["default"].string,
  highlightChoices: _propTypes["default"].bool,
  animationsDisabled: _propTypes["default"].bool,
  maxNoOfSelections: _propTypes["default"].number
});
//# sourceMappingURL=text-select.js.map