"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "Component", {
  enumerable: true,
  get: function get() {
    return _main["default"];
  }
});
exports["default"] = void 0;

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

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 _debug = _interopRequireDefault(require("debug"));

var _lodash = _interopRequireDefault(require("lodash"));

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

var _defaults = _interopRequireDefault(require("../configure/lib/defaults"));

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

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-ui:math-inline');

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

  var _super = _createSuper(MathInline);

  function MathInline() {
    var _this;

    (0, _classCallCheck2["default"])(this, MathInline);
    _this = _super.call(this);
    _this._configuration = _defaults["default"].configuration;
    _this.sessionChangedEventCaller = _lodash["default"].debounce(function () {
      _this.dispatchEvent(new _piePlayerEvents.SessionChangedEvent(_this.tagName.toLowerCase(), true));
    }, 1000);
    return _this;
  }

  (0, _createClass2["default"])(MathInline, [{
    key: "setLangAttribute",
    value: function setLangAttribute() {
      var language = this._model && (0, _typeof2["default"])(this._model.language) ? this._model.language : '';
      var lang = language ? language.slice(0, 2) : 'en';
      this.setAttribute('lang', lang); // set the lang attribute for the nearest parent div with class 'player-container' for MPI items as per PD-2483

      var playerContainer = this.closest('.player-container');

      if (playerContainer) {
        playerContainer.setAttribute('lang', lang);
      }
    }
  }, {
    key: "model",
    set: function set(m) {
      this._model = m;
      this.dispatchEvent(new _piePlayerEvents.ModelSetEvent(this._model, true, !!this._model));
      this.setLangAttribute();

      this._render();
    }
  }, {
    key: "session",
    get: function get() {
      return this._session;
    },
    set: function set(s) {
      this._session = s;

      this._render();
    }
  }, {
    key: "configuration",
    set: function set(c) {
      this._configuration = c;

      this._render();
    }
  }, {
    key: "sessionChanged",
    value: function sessionChanged(s) {
      var _this2 = this;

      Object.keys(s).map(function (key) {
        _this2._session[key] = s[key];
      });
      this.sessionChangedEventCaller();
      log('session: ', this._session);
    }
  }, {
    key: "connectedCallback",
    value: function connectedCallback() {
      this.setAttribute('aria-label', 'Math Response Question');
      this.setAttribute('role', 'region');

      this._render();
    }
  }, {
    key: "_render",
    value: function _render() {
      if (!this._model || !this._session) {
        return;
      }

      var el = /*#__PURE__*/_react["default"].createElement(_main["default"], {
        model: this._model,
        session: this._session,
        configuration: this._configuration,
        onSessionChange: this.sessionChanged.bind(this)
      });

      _reactDom["default"].render(el, this);
    }
  }]);
  return MathInline;
}( /*#__PURE__*/(0, _wrapNativeSuper2["default"])(HTMLElement));

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