"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 _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 _configure = _interopRequireDefault(require("./configure"));

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _pieConfigureEvents = require("@pie-framework/pie-configure-events");

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

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

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

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

  var _super = _createSuper(MathInlineConfigure);

  function MathInlineConfigure() {
    var _this;

    (0, _classCallCheck2["default"])(this, MathInlineConfigure);
    _this = _super.call(this);
    _this._model = MathInlineConfigure.createDefaultModel();
    _this._configuration = _defaults["default"].configuration;
    return _this;
  }

  (0, _createClass2["default"])(MathInlineConfigure, [{
    key: "model",
    set: function set(m) {
      this._model = MathInlineConfigure.createDefaultModel(m);

      this._render();
    }
  }, {
    key: "configuration",
    set: function set(c) {
      var _newConfiguration$lan,
          _this2 = this;

      var newConfiguration = _objectSpread(_objectSpread({}, _defaults["default"].configuration), c);

      this._configuration = newConfiguration; // if language:enabled is true, then the corresponding default item model should include a language value;
      // if it is false, then the language field should be omitted from the item model.
      // if a default item model includes a language value (e.g., en_US) and the corresponding authoring view settings have language:settings = true,
      // then (a) language:enabled should also be true, and (b) that default language value should be represented in languageChoices[] (as a key).

      if (newConfiguration !== null && newConfiguration !== void 0 && (_newConfiguration$lan = newConfiguration.language) !== null && _newConfiguration$lan !== void 0 && _newConfiguration$lan.enabled) {
        var _newConfiguration$lan2, _newConfiguration$lan3;

        if (newConfiguration !== null && newConfiguration !== void 0 && (_newConfiguration$lan2 = newConfiguration.languageChoices) !== null && _newConfiguration$lan2 !== void 0 && (_newConfiguration$lan3 = _newConfiguration$lan2.options) !== null && _newConfiguration$lan3 !== void 0 && _newConfiguration$lan3.length) {
          this._model.language = newConfiguration === null || newConfiguration === void 0 ? void 0 : newConfiguration.languageChoices.options[0].value;
        }
      } else if (newConfiguration.language.settings && this._model.language) {
        this._configuration.language.enabled = true;

        if (!this._configuration.languageChoices.options || !this._configuration.languageChoices.options.length) {
          this._configuration.languageChoices.options = [];
        } // check if the language is already included in the languageChoices.options array
        // and if not, then add it.


        if (!this._configuration.languageChoices.options.find(function (option) {
          return option.value === _this2._model.language;
        })) {
          this._configuration.languageChoices.options.push({
            value: this._model.language,
            label: this._model.language
          });
        }
      } else {
        delete this._model.language;
      }

      this._render();
    }
  }, {
    key: "onModelChanged",
    value: function onModelChanged(model) {
      this._model = model;
      log('[onModelChanged]: ', this._model);

      this._render();

      this.dispatchEvent(new _pieConfigureEvents.ModelUpdatedEvent(this._model));
    }
  }, {
    key: "onConfigurationChanged",
    value: function onConfigurationChanged(c) {
      this._configuration = c;

      if (this._model) {
        this.onModelChanged(this._model);
      }

      this._render();
    }
    /**
     *
     * @param {done, progress, file} handler
     */

  }, {
    key: "insertImage",
    value: function insertImage(handler) {
      this.dispatchEvent(new _pieConfigureEvents.InsertImageEvent(handler));
    }
  }, {
    key: "onDeleteImage",
    value: function onDeleteImage(src, done) {
      this.dispatchEvent(new _pieConfigureEvents.DeleteImageEvent(src, done));
    }
  }, {
    key: "insertSound",
    value: function insertSound(handler) {
      this.dispatchEvent(new _pieConfigureEvents.InsertSoundEvent(handler));
    }
  }, {
    key: "onDeleteSound",
    value: function onDeleteSound(src, done) {
      this.dispatchEvent(new _pieConfigureEvents.DeleteSoundEvent(src, done));
    }
  }, {
    key: "_render",
    value: function _render() {
      if (this._model) {
        var el = /*#__PURE__*/_react["default"].createElement(_configure["default"], {
          onModelChanged: this.onModelChanged.bind(this),
          onConfigurationChanged: this.onConfigurationChanged.bind(this),
          model: this._model,
          configuration: this._configuration,
          imageSupport: {
            add: this.insertImage.bind(this),
            "delete": this.onDeleteImage.bind(this)
          },
          uploadSoundSupport: {
            add: this.insertSound.bind(this),
            "delete": this.onDeleteSound.bind(this)
          }
        });

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

exports["default"] = MathInlineConfigure;
(0, _defineProperty2["default"])(MathInlineConfigure, "createDefaultModel", function () {
  var model = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  // making sure that defaults are set
  if (!(0, _isEmpty["default"])(model.responses)) {
    model.responses = model.responses.map(function (correctResponse) {
      return _objectSpread(_objectSpread({}, correctResponse), {}, {
        validation: correctResponse.validation || _defaults["default"].model.validationDefault,
        allowTrailingZeros: correctResponse.allowTrailingZeros || _defaults["default"].model.allowTrailingZerosDefault,
        ignoreOrder: correctResponse.ignoreOrder || _defaults["default"].model.ignoreOrderDefault || false
      });
    });
  }

  return _objectSpread(_objectSpread({}, _defaults["default"].model), model);
});
//# sourceMappingURL=index.js.map