"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _partial = _interopRequireDefault(require("lodash/partial"));

var _classnames = _interopRequireDefault(require("classnames"));

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _immutable = _interopRequireDefault(require("immutable"));

var _IconEdit = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconEdit"));

var _IconPlus = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconPlus"));

var _IconCopy = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconCopy"));

var _IconTrash = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconTrash"));

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

var _Heading = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Heading"));

var _PresentationContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/PresentationContent"));

var _Alert = _interopRequireDefault(require("@instructure/ui-alerts/lib/components/Alert"));

var _List = _interopRequireWildcard(require("@instructure/ui-elements/lib/components/List"));

var _Text = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Text"));

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

var _lib = _interopRequireDefault(require("@instructure/ui-themeable/lib"));

var _RichContentRenderer = _interopRequireDefault(require("@instructure/quiz-rce/lib/components/RichContentRenderer"));

var _extractTextFromHtml = _interopRequireDefault(require("quiz-interactions/lib/util/extractTextFromHtml"));

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var _withI18nSupport2 = _interopRequireDefault(require("@instructure/quiz-common/lib/with-i18n-support"));

var _CopyMoveBankEntryModal = _interopRequireDefault(require("../CopyMoveBankEntryModal"));

var _ExpandButton = _interopRequireDefault(require("../../../common/components/shared/ExpandButton"));

var _QuizEntry = _interopRequireDefault(require("../../../common/records/QuizEntry"));

var _Overlay = _interopRequireDefault(require("../../../common/components/shared/overlay/Overlay"));

var _ItemShow = _interopRequireDefault(require("../../../common/components/resources/item/ItemShow"));

var _StimulusShowInfo = _interopRequireDefault(require("../../../common/components/resources/stimulus/StimulusShowInfo"));

var _TagDisplay = _interopRequireDefault(require("../../../common/components/shared/TagDisplay"));

var _constants = require("@instructure/quiz-common/lib/constants");

var _theme = _interopRequireDefault(require("./theme"));

var _dec, _class, _class2, _temp;

var styles = {
  componentId: 'eQXFj',
  template: function template(theme) {
    return "\n\n.quiz-core__BankEntryRow--entryDetail {\n  padding: ".concat(theme.entryDetailPadding || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--headerRow {\n  display: flex;\n  flex-direction: row;\n  justify-content: space-between;\n  align-items: flex-start;\n  color: ").concat(theme.headerRowColor || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--buttons {\n  position: relative;\n  display: inline-flex;\n  z-index: ").concat(theme.buttonsZIndex || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--expandButtonContainer {\n  border-left: ").concat(theme.expandButtonContainerBorderWidth || 'inherit', " solid ").concat(theme.expandButtonContainerBorderColor || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--deleteAlert {\n  display: block;\n}\n\n.quiz-core__BankEntryRow--deletePreface {\n  margin-bottom: ").concat(theme.deletePrefaceMargin || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--addButton {\n  margin-bottom: ").concat(theme.addButtonMargin || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--headerText {\n  color: ").concat(theme.headerTextColor || 'inherit', ";\n  font-size: ").concat(theme.headerTextFontSize || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--stem {\n  overflow-x: auto;\n  font-size: ").concat(theme.stemFontSize || 'inherit', ";\n  line-height: ").concat(theme.stemLineHeight || 'inherit', ";\n}\n\n.quiz-core__BankEntryRow--stem p {\n    margin: 0;\n  }\n\n.quiz-core__BankEntryRow--footer {\n  padding: ").concat(theme.footerPadding || 'inherit', ";\n  border-top: ").concat(theme.footerBorderWidth || 'inherit', " solid ").concat(theme.footerBorderColor || 'inherit', ";\n  font-size: ").concat(theme.footerFontSize || 'inherit', ";\n}\n");
  },
  'entryDetail': 'quiz-core__BankEntryRow--entryDetail',
  'headerRow': 'quiz-core__BankEntryRow--headerRow',
  'buttons': 'quiz-core__BankEntryRow--buttons',
  'expandButtonContainer': 'quiz-core__BankEntryRow--expandButtonContainer',
  'deleteAlert': 'quiz-core__BankEntryRow--deleteAlert',
  'deletePreface': 'quiz-core__BankEntryRow--deletePreface',
  'addButton': 'quiz-core__BankEntryRow--addButton',
  'headerText': 'quiz-core__BankEntryRow--headerText',
  'stem': 'quiz-core__BankEntryRow--stem',
  'footer': 'quiz-core__BankEntryRow--footer'
};
var BankEntryRow = (_dec = (0, _lib.default)(_theme.default, styles), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_withI18nSupport) {
  (0, _inherits2.default)(BankEntryRow, _withI18nSupport);

  function BankEntryRow() {
    var _getPrototypeOf2;

    var _this;

    (0, _classCallCheck2.default)(this, BankEntryRow);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(BankEntryRow)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.incrementPosition = function () {
      _this.props.setUi(_constants.BUILD_TRAY_POSITION, _this.props.position + 1);
    };

    _this.deleteBankEntry = function () {
      var _params, _s;

      var confirmBody = _react.default.createElement(_Alert.default, {
        variant: "error",
        padding: "0"
      }, _react.default.createElement("div", {
        className: styles.deleteAlert
      }, _react.default.createElement("div", {
        className: styles.deletePreface
      }, "Sletter dette elementet:"), _react.default.createElement(_List.default, null, _react.default.createElement(_List.ListItem, null, (_params = [function (_ref) {
        var children = _ref.children;
        return _react.default.createElement(_Text.default, {
          weight: "bold",
          key: "0"
        }, children);
      }], ["vil ", (_s = _params["0"], typeof _s === "function" ? _s({
        children: ["IKKE"]
      }) : _s), " fjerne det fra noen vurderinger"])), _react.default.createElement(_List.ListItem, null, "vil miste sin statistiske lenke under rapportering"), _react.default.createElement(_List.ListItem, null, "vil fjerne tilgang for alle brukere denne banken har blitt delt med"))));

      _this.props.withConfirm((0, _partial.default)(_this.props.removeBankEntry, _this.props.bankId, _this.props.bankEntry.id), {
        title: "Slett element",
        text: confirmBody,
        continueText: "Slett",
        continueVariant: 'danger',
        size: 'medium'
      });
    };

    _this.addToQuiz = function () {
      if (_this.props.activeQuizId) {
        var newEntry = _QuizEntry.default.create({
          entryId: _this.props.bankEntry.id,
          entryType: 'BankEntry',
          position: _this.props.position,
          stimulusQuizEntryId: _this.props.stimulusId
        });

        _this.props.addEntryToQuiz(_this.props.activeQuizId, newEntry.entryId, newEntry.entryType, newEntry.getProperties(), false).then(function () {
          _this.props.screenreaderNotification("Bankinnlegg ble lagt til testen");
        });

        if (_this.props.position) _this.incrementPosition();
      }
    };

    _this.openCopyMoveBankEntryModal = function () {
      _this.props.openModal([_constants.COPY_MOVE_BANK_ENTRY_MODAL, _this.props.bankEntry.entryType, _this.props.bankEntry.entry.toJS().id].join(':'), {
        modalTrigger: _this.copyMoveBankEntryButton
      });
    };

    _this.createEntryForBank = function () {
      var createEntry = _this.isItemEntry() ? _this.props.createItem : _this.props.createStimulus;
      return createEntry(_this.getEntry(), _this.props.bankId, 'bank');
    };

    _this.storeCopyMoveBankEntryButtonRef = function (c) {
      _this.copyMoveBankEntryButton = c;
    };

    _this.storeEditButtonRef = function (c) {
      _this.editButton = c;
    };

    _this.renderTrashButton = function () {
      var title = "Slett " + _this.type();

      return _react.default.createElement(_Button.default, {
        variant: "icon",
        size: "medium",
        onClick: _this.deleteBankEntry,
        "data-automation": "sdk-delete-bank-entry-button",
        icon: _IconTrash.default,
        title: title
      }, _react.default.createElement(_ScreenReaderContent.default, null, title));
    };

    _this.navigateToBankEntry = function () {
      _this.props.setUi(_constants.SET_EDIT_BUTTON_FOCUS, _this.props.bankEntry.id);

      _this.props.navigateToBankEntry(_this.props.bankEntry.id, _this.props.bankId);
    };

    return _this;
  }

  (0, _createClass2.default)(BankEntryRow, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.bankEntry.id === this.props.setEditButtonFocus) {
        this.editButton.focus();
        this.props.clearUi(_constants.SET_EDIT_BUTTON_FOCUS);
      }
    }
  }, {
    key: "getEntry",
    value: function getEntry() {
      return this.props.bankEntry.getEntry();
    }
  }, {
    key: "isItemEntry",
    value: function isItemEntry() {
      return this.props.bankEntry.entryType === 'Item';
    }
  }, {
    key: "type",
    value: function type() {
      return this.isItemEntry() ? "Element" : "Stimulus";
    }
  }, {
    key: "entryIdentifier",
    value: function entryIdentifier() {
      return this.isItemEntry() ? "Element " + (0, _extractTextFromHtml.default)(this.getEntry().title) : "Stimulus " + this.getEntry().title;
    }
  }, {
    key: "moveModalTitle",
    value: function moveModalTitle() {
      return "Kopier / Flytt " + this.type();
    }
  }, {
    key: "renderExpandButton",
    value: function renderExpandButton() {
      var type = this.type();
      var title = this.props.isExpanded ? "Skjul " + type + " detaljer" : "Utvid " + type + " detaljer";
      return _react.default.createElement("div", {
        className: styles.expandButtonContainer
      }, _react.default.createElement(_ExpandButton.default, {
        onClick: this.props.toggleExpanded,
        title: title,
        isExpanded: this.props.isExpanded
      }));
    }
  }, {
    key: "renderAddButton",
    value: function renderAddButton() {
      if (this.props.addEntryToQuiz) {
        var title = "Legg til " + this.type() + " i denne quizen";
        return _react.default.createElement("div", {
          className: (0, _classnames.default)(styles.buttons, styles.addButton)
        }, _react.default.createElement(_Button.default, {
          variant: "circle-primary",
          size: "small",
          onClick: this.addToQuiz,
          "data-automation": "sdk-add-bank-entry-button",
          disabled: this.props.bankEntry.isStimulus && this.props.stimulusId !== null,
          icon: _IconPlus.default,
          title: title
        }, _react.default.createElement(_ScreenReaderContent.default, null, title)));
      } // This empty div is necessary for spacing


      return _react.default.createElement("div", null);
    }
  }, {
    key: "renderMoveButton",
    value: function renderMoveButton() {
      var title = this.moveModalTitle();
      return _react.default.createElement(_Button.default, {
        ref: this.storeCopyMoveBankEntryButtonRef,
        variant: "icon",
        size: "medium",
        onClick: this.openCopyMoveBankEntryModal,
        "data-automation": "sdk-copy-move-bank-entry-button",
        icon: _IconCopy.default,
        title: title
      }, _react.default.createElement(_ScreenReaderContent.default, null, title));
    }
  }, {
    key: "entryHeader",
    value: function entryHeader() {
      return this.isItemEntry() ? this.getEntry().getInteractionType().name : "Stimulus";
    }
  }, {
    key: "renderHeader",
    value: function renderHeader() {
      var separator = ' | ';
      return _react.default.createElement("div", null, _react.default.createElement("div", {
        className: styles.headerRow
      }, this.renderAddButton(), _react.default.createElement("div", {
        className: styles.buttons
      }, this.renderEditButton(), this.renderMoveButton(), this.renderTrashButton(), this.renderExpandButton())), _react.default.createElement("div", {
        className: styles.headerText
      }, _react.default.createElement(_Heading.default, {
        level: "reset",
        as: "h2"
      }, this.entryHeader(), _react.default.createElement(_PresentationContent.default, null, separator), this.entryIdentifier())));
    }
  }, {
    key: "renderEntryCollapsed",
    value: function renderEntryCollapsed() {
      var itemOrStimulus = this.getEntry(); // render item's body as html
      // or title plain text

      if (itemOrStimulus.itemBody) {
        return _react.default.createElement(_RichContentRenderer.default, {
          customStyles: styles.stem,
          content: itemOrStimulus.itemBody
        });
      } else {
        return _react.default.createElement("div", {
          className: styles.stem
        }, itemOrStimulus.title);
      }
    }
  }, {
    key: "renderEntryExpanded",
    value: function renderEntryExpanded() {
      var entry = this.getEntry();

      if (this.isItemEntry()) {
        return _react.default.createElement(_ItemShow.default, {
          entry: entry,
          guid: this.props.bankEntry.id
        });
      } else {
        return _react.default.createElement(_StimulusShowInfo.default, {
          stimulus: entry
        });
      }
    }
  }, {
    key: "renderEditButton",
    value: function renderEditButton() {
      var title = "Rediger " + this.type();
      return _react.default.createElement(_Button.default, {
        ref: this.storeEditButtonRef,
        variant: "icon",
        size: "medium",
        onClick: this.navigateToBankEntry,
        "data-automation": "sdk-edit-bank-entry-button",
        icon: _IconEdit.default,
        title: title
      }, _react.default.createElement(_ScreenReaderContent.default, null, title));
    }
  }, {
    key: "renderTags",
    value: function renderTags() {
      if (!this.props.useBankTags) {
        return null;
      }

      return _react.default.createElement(_TagDisplay.default, {
        tagAssociations: this.props.tagAssociations
      });
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      var lastUpdate = [function (_ref2) {
        var children = _ref2.children;
        return _react.default.createElement(_Text.default, {
          key: "lastUpdate",
          weight: "bold"
        }, children);
      }({
        children: ["Siste oppdatering"]
      }), " ", this.formatDatetimeMedium(this.props.bankEntry.get('updatedAt'))];
      return _react.default.createElement("div", {
        className: styles.footer
      }, _react.default.createElement("span", null, lastUpdate), this.renderTags());
    }
  }, {
    key: "render",
    value: function render() {
      var isExpanded = this.props.isExpanded;
      return _react.default.createElement(_Overlay.default, {
        onClick: this.props.toggleExpanded,
        tabIndex: "-1",
        ariaLabel: this.entryIdentifier()
      }, _react.default.createElement("div", {
        role: "group"
      }, _react.default.createElement("div", {
        className: styles.entryDetail,
        "data-automation": "sdk-bankEntryItem"
      }, this.renderHeader(), isExpanded ? this.renderEntryExpanded() : this.renderEntryCollapsed()), isExpanded && this.renderFooter()), _react.default.createElement(_CopyMoveBankEntryModal.default, {
        createEntry: this.createEntryForBank,
        label: this.moveModalTitle(),
        sourceBankId: this.props.bankId,
        sourceBankEntryId: this.props.bankEntry.id,
        entryId: this.props.bankEntry.entry.get('id'),
        entryType: this.props.bankEntry.entryType
      }));
    }
  }]);
  BankEntryRow.displayName = "BankEntryRow";
  return BankEntryRow;
}((0, _withI18nSupport2.default)(_react.Component)), _class2.propTypes = {
  activeQuizId: _propTypes.default.string,
  addEntryToQuiz: _propTypes.default.func,
  bankEntry: _reactImmutableProptypes.default.record.isRequired,
  bankId: _propTypes.default.string.isRequired,
  clearUi: _propTypes.default.func.isRequired,
  createItem: _propTypes.default.func.isRequired,
  createStimulus: _propTypes.default.func.isRequired,
  isExpanded: _propTypes.default.bool,
  navigateToBankEntry: _propTypes.default.func.isRequired,
  openModal: _propTypes.default.func.isRequired,
  position: _propTypes.default.number,
  removeBankEntry: _propTypes.default.func.isRequired,
  screenreaderNotification: _propTypes.default.func.isRequired,
  setEditButtonFocus: _propTypes.default.bool,
  setUi: _propTypes.default.func.isRequired,
  stimulusId: _propTypes.default.number,
  tagAssociations: _reactImmutableProptypes.default.listOf(_reactImmutableProptypes.default.map),
  useBankTags: _propTypes.default.bool.isRequired,
  toggleExpanded: _propTypes.default.func.isRequired,
  withConfirm: _propTypes.default.func.isRequired,
  locale: _propTypes.default.string,
  timezone: _propTypes.default.string
}, _class2.defaultProps = {
  activeQuizId: null,
  addEntryToQuiz: null,
  stimulusId: null,
  isExpanded: false,
  position: 0,
  setEditButtonFocus: false,
  tagAssociations: _immutable.default.List(),
  locale: null,
  timezone: null
}, _class2.contextTypes = {
  locale: _propTypes.default.string,
  timezone: _propTypes.default.string
}, _temp)) || _class);
exports.default = BankEntryRow;