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

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

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

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

var _debounce = _interopRequireDefault(require("lodash/debounce"));

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

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

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

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; } }

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; }

var log = (0, _debug["default"])('pie-element:math-inline:print');
/**
 * Live in same package as main element - so we can access some of the shared comps!
 *
 * - update pslb to build print if src/print.js is there
 * - update demo el
 * - get configure/controller building
 */

var preparePrintModel = function preparePrintModel(model, opts) {
  var instr = opts.role === 'instructor';
  model.prompt = model.promptEnabled !== false ? model.prompt : undefined;
  model.teacherInstructions = instr && model.teacherInstructionsEnabled !== false ? model.teacherInstructions : undefined;
  model.rationale = instr && model.rationaleEnabled !== false ? model.rationale : undefined;
  model.showTeacherInstructions = instr;
  model.alwaysShowCorrect = instr;
  model.printMode = true;
  model.feedback = undefined;
  model.expression = model.expression || '{{response}}';
  model.animationsDisabled = true;
  return _objectSpread(_objectSpread({}, model), {}, {
    disabled: true,
    config: _objectSpread(_objectSpread({}, model), {}, {
      env: {
        mode: instr ? 'evaluate' : model.mode,
        role: opts.role
      }
    })
  });
};

var MathInlinePrint = /*#__PURE__*/function (_HTMLElement) {
  (0, _inherits2["default"])(MathInlinePrint, _HTMLElement);

  var _super = _createSuper(MathInlinePrint);

  function MathInlinePrint() {
    var _this;

    (0, _classCallCheck2["default"])(this, MathInlinePrint);
    _this = _super.call(this);
    _this._options = null;
    _this._model = null;
    _this._session = [];
    _this._rerender = (0, _debounce["default"])(function () {
      if (_this._model && _this._session) {
        var printModel = preparePrintModel(_this._model, _this._options);

        var element = _this._options && /*#__PURE__*/_react["default"].createElement(_main["default"], {
          model: printModel,
          session: {}
        });

        _reactDom["default"].render(element, (0, _assertThisInitialized2["default"])(_this), function () {
          log('render complete - render math');
          (0, _mathRendering.renderMath)((0, _assertThisInitialized2["default"])(_this));
        });
      } else {
        log('skip');
      }
    }, 50, {
      leading: false,
      trailing: true
    });
    return _this;
  }

  (0, _createClass2["default"])(MathInlinePrint, [{
    key: "options",
    set: function set(o) {
      this._options = o;
    }
  }, {
    key: "model",
    set: function set(s) {
      this._model = s;

      this._rerender();
    }
  }, {
    key: "connectedCallback",
    value: function connectedCallback() {}
  }]);
  return MathInlinePrint;
}( /*#__PURE__*/(0, _wrapNativeSuper2["default"])(HTMLElement));

exports["default"] = MathInlinePrint;
//# sourceMappingURL=print.js.map