"use strict";

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

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

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

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

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

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

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

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

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

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

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

var _lib = _interopRequireDefault(require("@pie-element/rubric/configure/lib"));

var _lib2 = _interopRequireDefault(require("@pie-element/multi-trait-rubric/configure/lib"));

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

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

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

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

var _excluded = ["rubrics"];

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 MODEL_UPDATED = _pieConfigureEvents.ModelUpdatedEvent.TYPE;
var RUBRIC_TAG_NAME = 'rubric-configure';
var MULTI_TRAIT_RUBRIC_TAG_NAME = 'multi-trait-rubric-configure';

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

  var _super = _createSuper(ComplexSimpleRubricConfigure);

  function ComplexSimpleRubricConfigure() {
    (0, _classCallCheck2["default"])(this, ComplexSimpleRubricConfigure);
    return _super.apply(this, arguments);
  }

  return (0, _createClass2["default"])(ComplexSimpleRubricConfigure);
}(_lib["default"]);

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

  var _super2 = _createSuper(ComplexMTRConfigure);

  function ComplexMTRConfigure() {
    (0, _classCallCheck2["default"])(this, ComplexMTRConfigure);
    return _super2.apply(this, arguments);
  }

  return (0, _createClass2["default"])(ComplexMTRConfigure);
}(_lib2["default"]);

var defineComplexRubric = function defineComplexRubric() {
  if (!customElements.get(RUBRIC_TAG_NAME)) {
    customElements.define(RUBRIC_TAG_NAME, ComplexSimpleRubricConfigure);
  }

  if (!customElements.get(MULTI_TRAIT_RUBRIC_TAG_NAME)) {
    customElements.define(MULTI_TRAIT_RUBRIC_TAG_NAME, ComplexMTRConfigure);
  }
};

defineComplexRubric();

var prepareCustomizationObject = function prepareCustomizationObject(config, model) {
  var configuration = (0, _defaults["default"])(config, _defaults2["default"].configuration);
  return {
    configuration: configuration,
    model: model
  };
};

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

  var _super3 = _createSuper(ComplexRubricConfigureElement);

  function ComplexRubricConfigureElement() {
    var _this;

    (0, _classCallCheck2["default"])(this, ComplexRubricConfigureElement);
    _this = _super3.call(this);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onModelChanged", function (m, reset) {
      _this._model = ComplexRubricConfigureElement.createDefaultModel(m, _this._model);

      _this.dispatchModelUpdated(reset);

      _this._render();
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onModelUpdated", function (e) {
      if (e.target === (0, _assertThisInitialized2["default"])(_this)) {
        return;
      }

      e.preventDefault();
      e.stopImmediatePropagation();
      var id = e.target && e.target.getAttribute('id');

      if (id) {
        if (e.update) {
          _this._model.rubrics[id] = e.update;
        }

        _this.dispatchEvent(new _pieConfigureEvents.ModelUpdatedEvent(_this._model));
      }
    });
    _this.canUpdateModel = false;

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

    _this._model = ComplexRubricConfigureElement.createDefaultModel();
    _this._configuration = _defaults2["default"].configuration;
    _this.onConfigurationChanged = _this.onConfigurationChanged.bind((0, _assertThisInitialized2["default"])(_this));
    return _this;
  }

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

      this._render();
    }
  }, {
    key: "dispatchModelUpdated",
    value: function dispatchModelUpdated(reset) {
      var resetValue = !!reset;
      this.dispatchEvent(new _pieConfigureEvents.ModelUpdatedEvent(this._model, resetValue));
    }
  }, {
    key: "configuration",
    set: function set(c) {
      var info = prepareCustomizationObject(c, this._model);
      this._configuration = info.configuration;

      this._render();
    }
  }, {
    key: "onConfigurationChanged",
    value: function onConfigurationChanged(c) {
      this._configuration = prepareCustomizationObject(c, this._model).configuration;

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

      this._render();
    }
  }, {
    key: "connectedCallback",
    value: function connectedCallback() {
      this.addEventListener(MODEL_UPDATED, this.onModelUpdated);

      this._render();
    }
  }, {
    key: "disconnectedCallback",
    value: function disconnectedCallback() {
      this.removeEventListener(MODEL_UPDATED, this.onModelUpdated);
    }
  }, {
    key: "_render",
    value: function _render() {
      var element = /*#__PURE__*/_react["default"].createElement(_main["default"], {
        model: this._model,
        configuration: this._configuration,
        onModelChanged: this.onModelChanged,
        onConfigurationChanged: this.onConfigurationChanged,
        canUpdateModel: this.canUpdateModel
      });

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

exports["default"] = ComplexRubricConfigureElement;
(0, _defineProperty2["default"])(ComplexRubricConfigureElement, "createDefaultModel", function () {
  var _sensibleDefaults$mod, _sensibleDefaults$mod2, _sensibleDefaults$mod3;

  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref$rubrics = _ref.rubrics;

  _ref$rubrics = _ref$rubrics === void 0 ? {
    simpleRubric: {},
    rubricless: {},
    multiTraitRubric: {}
  } : _ref$rubrics;
  var _ref$rubrics$simpleRu = _ref$rubrics.simpleRubric,
      simpleRubric = _ref$rubrics$simpleRu === void 0 ? {} : _ref$rubrics$simpleRu,
      _ref$rubrics$rubricle = _ref$rubrics.rubricless,
      rubricless = _ref$rubrics$rubricle === void 0 ? {} : _ref$rubrics$rubricle,
      _ref$rubrics$multiTra = _ref$rubrics.multiTraitRubric,
      multiTraitRubric = _ref$rubrics$multiTra === void 0 ? {} : _ref$rubrics$multiTra,
      model = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
  var currentModel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var pieDefaults = (_defaults2["default"] === null || _defaults2["default"] === void 0 ? void 0 : _defaults2["default"].model) || {};
  return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, pieDefaults), currentModel), model), {}, {
    rubrics: {
      simpleRubric: _objectSpread(_objectSpread(_objectSpread({}, ((_defaults2["default"] === null || _defaults2["default"] === void 0 ? void 0 : (_sensibleDefaults$mod = _defaults2["default"].model) === null || _sensibleDefaults$mod === void 0 ? void 0 : _sensibleDefaults$mod.rubrics) || {}).simpleRubric), (currentModel.rubrics || {}).simpleRubric), simpleRubric),
      multiTraitRubric: _objectSpread(_objectSpread(_objectSpread({}, ((_defaults2["default"] === null || _defaults2["default"] === void 0 ? void 0 : (_sensibleDefaults$mod2 = _defaults2["default"].model) === null || _sensibleDefaults$mod2 === void 0 ? void 0 : _sensibleDefaults$mod2.rubrics) || {}).multiTraitRubric), (currentModel.rubrics || {}).multiTraitRubric), multiTraitRubric),
      rubricless: _objectSpread(_objectSpread(_objectSpread({}, ((_defaults2["default"] === null || _defaults2["default"] === void 0 ? void 0 : (_sensibleDefaults$mod3 = _defaults2["default"].model) === null || _sensibleDefaults$mod3 === void 0 ? void 0 : _sensibleDefaults$mod3.rubrics) || {}).rubricless), (currentModel.rubrics || {}).rubricless), rubricless)
    }
  });
});
//# sourceMappingURL=index.js.map