"use strict";

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

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

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

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 _pieConfigureEvents = require("@pie-framework/pie-configure-events");

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

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

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

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

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

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 modelWithDefaults = function modelWithDefaults(m) {
  return _objectSpread(_objectSpread({}, _defaults["default"].model), m);
};

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

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

  var _super = _createSuper(RubricElement);

  function RubricElement() {
    var _this;

    (0, _classCallCheck2["default"])(this, RubricElement);
    _this = _super.call(this);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "updateModelAccordingToReceivedProps", function (nextModel) {
      var currentModel = _objectSpread({}, _this._model);

      if (!nextModel) {
        return currentModel;
      }

      var validatedModel = _objectSpread({}, nextModel); // excludeZero should be false and disabled when maxPoints is 1


      if (validatedModel.maxPoints === 1) {
        validatedModel.excludeZero = false;
      }

      var _ref = validatedModel || {},
          maxPoints = _ref.maxPoints,
          excludeZero = _ref.excludeZero;

      validatedModel.points = validatedModel.points ? (0, _toConsumableArray2["default"])(validatedModel.points) : [];
      validatedModel.sampleAnswers = validatedModel.sampleAnswers ? (0, _toConsumableArray2["default"])(validatedModel.sampleAnswers) : [];
      var howManyPointsShouldHave = excludeZero ? maxPoints : maxPoints + 1;
      var howManyPointsDoesItHave = validatedModel.points.length;
      var excludeZeroChanged = (currentModel === null || currentModel === void 0 ? void 0 : currentModel.excludeZero) !== (nextModel === null || nextModel === void 0 ? void 0 : nextModel.excludeZero);
      var maxPointsChanged = (currentModel === null || currentModel === void 0 ? void 0 : currentModel.maxPoints) !== (nextModel === null || nextModel === void 0 ? void 0 : nextModel.maxPoints);

      if (howManyPointsDoesItHave < howManyPointsShouldHave) {
        if (excludeZeroChanged && !excludeZero) {
          validatedModel.points = [''].concat((0, _toConsumableArray2["default"])(validatedModel.points));
          validatedModel.sampleAnswers = [null].concat((0, _toConsumableArray2["default"])(validatedModel.sampleAnswers));
        }

        if (maxPointsChanged) {
          for (var i = 0; i < howManyPointsShouldHave - howManyPointsDoesItHave; i++) {
            validatedModel.points.push('');
            validatedModel.sampleAnswers.push(null);
          }
        }
      } else if (howManyPointsDoesItHave > howManyPointsShouldHave) {
        if (excludeZeroChanged && excludeZero) {
          validatedModel.points = validatedModel.points.slice(1);
          validatedModel.sampleAnswers = validatedModel.sampleAnswers.slice(1);
        }

        if (maxPointsChanged) {
          validatedModel.points = validatedModel.points.slice(0, howManyPointsShouldHave);
          validatedModel.sampleAnswers = validatedModel.sampleAnswers.slice(0, howManyPointsShouldHave);
        }
      }

      return validatedModel;
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onConfigurationChanged", function (c) {
      _this._configuration = configurationWithDefaults(c);

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

      _this._render();
    });

    _debug["default"].log('constructor called');

    _this._model = modelWithDefaults();
    _this._configuration = configurationWithDefaults();
    _this.onModelChanged = _this.onModelChanged.bind((0, _assertThisInitialized2["default"])(_this));
    _this.onConfigurationChanged = _this.onConfigurationChanged.bind((0, _assertThisInitialized2["default"])(_this));
    return _this;
  }

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

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

      this._render();
    }
  }, {
    key: "onModelChanged",
    value: function onModelChanged(m) {
      this._model = this.updateModelAccordingToReceivedProps(m);

      this._render();

      this.dispatchEvent(new _pieConfigureEvents.ModelUpdatedEvent(this._model, false));
    }
  }, {
    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: "connectedCallback",
    value: function connectedCallback() {
      this._render();
    }
  }, {
    key: "_render",
    value: function _render() {
      if (this._model) {
        var element = /*#__PURE__*/_react["default"].createElement(_main["default"], {
          model: this._model,
          configuration: this._configuration,
          onModelChanged: this.onModelChanged,
          onConfigurationChanged: this.onConfigurationChanged,
          imageSupport: {
            add: this.insertImage.bind(this),
            "delete": this.onDeleteImage.bind(this)
          }
        });

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

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