"use strict";

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

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

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

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

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

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 _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _piePlayerEvents = require("@pie-framework/pie-player-events");

var _main = _interopRequireDefault(require("./main"));

var _mathRenderingAccessible = require("@pie-lib/pie-toolbox/math-rendering-accessible");

var _utils = _interopRequireDefault(require("./utils"));

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

  var _super = _createSuper(SelectText);

  function SelectText() {
    (0, _classCallCheck2["default"])(this, SelectText);
    return _super.call(this);
  }

  (0, _createClass2["default"])(SelectText, [{
    key: "model",
    set: function set(m) {
      this._model = m;
      this.render();
      this.dispatchEvent(new _piePlayerEvents.ModelSetEvent(this.tagName.toLowerCase(), this.isSessionComplete(), this._model !== undefined));
    }
  }, {
    key: "session",
    get: function get() {
      return this._session;
    },
    set: function set(s) {
      this._session = s;

      if (this._model) {
        var generatedModel = (0, _utils["default"])(this._model) || {};
        var tokens = generatedModel.tokens;

        var _ref = s || {},
            selectedTokens = _ref.selectedTokens;

        selectedTokens = selectedTokens || []; // This case was introduced mostly for createCorrectSession:
        // make sure initial session tokens are parsed and have correct start and end (according to the regenerated model)

        var selectedTokenExistsInGeneratedTokens = function selectedTokenExistsInGeneratedTokens(s) {
          return tokens.find(function (_ref2) {
            var tStart = _ref2.start,
                tEnd = _ref2.end;
            return s.start === tStart && s.end === tEnd;
          });
        }; // check if ALL the selectedTokens have the correct start and end


        var allAreCorrect = selectedTokens.reduce(function (acc, selectedToken) {
          return acc && selectedTokenExistsInGeneratedTokens(selectedToken);
        }, true);

        if (!allAreCorrect) {
          // otherwise, make sure to parse selectedTokens as well
          var _generatedModel = (0, _utils["default"])(_objectSpread(_objectSpread({}, this._model), {}, {
            tokens: selectedTokens
          })) || {};

          var _tokens = _generatedModel.tokens;

          if (_tokens) {
            this._session.selectedTokens = _tokens;
          }
        }
      }

      if (!Array.isArray(this._session.selectedTokens)) {
        this._session.selectedTokens = [];
      }

      this.render();
    }
  }, {
    key: "isSessionComplete",
    value: function isSessionComplete() {
      var _ref3 = this._session || {},
          selectedTokens = _ref3.selectedTokens;

      return Array.isArray(selectedTokens) && selectedTokens.length > 0;
    }
  }, {
    key: "selectionChanged",
    value: function selectionChanged(selection) {
      this._session.selectedTokens = selection;
      this.dispatchEvent(new _piePlayerEvents.SessionChangedEvent(this.tagName.toLowerCase(), this.isSessionComplete()));
    }
  }, {
    key: "connectedCallback",
    value: function connectedCallback() {
      this.render();
    }
  }, {
    key: "render",
    value: function render() {
      var _this = this;

      if (this._model && this._session) {
        var el = /*#__PURE__*/_react["default"].createElement(_main["default"], {
          model: this._model,
          session: this._session,
          onSelectionChange: this.selectionChanged.bind(this)
        });

        _reactDom["default"].render(el, this, function () {
          (0, _mathRenderingAccessible.renderMath)(_this);
        });
      }
    }
  }]);
  return SelectText;
}( /*#__PURE__*/(0, _wrapNativeSuper2["default"])(HTMLElement));

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