"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.deleteTagAssociations = exports.createTagAssociations = exports.getTagSuggestions = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

var _Fetcher = _interopRequireDefault(require("../../common/util/Fetcher"));

var _helpers = require("../../common/api/helpers");

var _tagAssociations = require("../../common/actions/tagAssociations");

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

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

var getTagSuggestions = function getTagSuggestions(entryType, entryId, text) {
  return function (dispatch, getState) {
    var url = '/api/tags';
    var urlParams = {
      filter: text
    };
    var fetcher = new _Fetcher.default({
      callType: _constants.GET_TAG_SUGGESTIONS,
      onError: (0, _helpers.handleError)(dispatch, "Kunne ikke s\xF8ge tags.")
    });
    return fetcher.get(url, urlParams).then(function (tags) {
      return dispatch((0, _tagAssociations.setTagSuggestions)(entryType, entryId, tags, text));
    });
  };
};

exports.getTagSuggestions = getTagSuggestions;

var createTagAssociation = function createTagAssociation(dispatch, bankEntryId, entityType, entityId, tagValue) {
  var url = "/api/bank_entries/".concat(bankEntryId, "/tag_associations");
  var fetcher = new _Fetcher.default({
    callType: _constants.CREATE_TAG_ASSOCIATION,
    onError: (0, _helpers.handleError)(dispatch, "Kunne ikke oprette tag.")
  });
  return fetcher.post(url, {
    body: JSON.stringify({
      tag_value: tagValue
    })
  }).then(function (tagAssociation) {
    return dispatch((0, _tagAssociations.addTagAssociation)(entityType, entityId, tagAssociation));
  });
};

var createTagAssociations = function createTagAssociations(bankEntryId, entityType, entityId, tagValues) {
  return function (dispatch, getState) {
    return Promise.all(tagValues.map(
    /*#__PURE__*/
    function () {
      var _ref = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee(tagValue) {
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return createTagAssociation(dispatch, bankEntryId, entityType, entityId, tagValue);

              case 2:
                return _context.abrupt("return", dispatch((0, _tagAssociations.removePendingAdditionTagAssociation)(entityType, entityId, tagValue)));

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }()));
  };
};

exports.createTagAssociations = createTagAssociations;

var deleteTagAssociation = function deleteTagAssociation(dispatch, bankEntryId, entityType, entityId, tagAssociationId) {
  var url = "/api/bank_entries/".concat(bankEntryId, "/tag_associations/").concat(tagAssociationId);
  var fetcher = new _Fetcher.default({
    callType: _constants.DELETE_TAG_ASSOCIATION,
    onError: (0, _helpers.handleError)(dispatch, "Kunne ikke slette tag.")
  });
  return fetcher.delete(url).then(function (response) {
    return dispatch([(0, _tagAssociations.removeTagAssociation)(entityType, entityId, tagAssociationId)]);
  });
};

var deleteTagAssociations = function deleteTagAssociations(bankEntryId, entityType, entityId, tagAssociationIds) {
  return function (dispatch, getState) {
    return Promise.all(tagAssociationIds.map(
    /*#__PURE__*/
    function () {
      var _ref2 = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2(tagAssociationId) {
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return deleteTagAssociation(dispatch, bankEntryId, entityType, entityId, tagAssociationId);

              case 2:
                return _context2.abrupt("return", dispatch((0, _tagAssociations.removePendingDeletionTagAssociation)(entityType, entityId, tagAssociationId)));

              case 3:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      return function (_x2) {
        return _ref2.apply(this, arguments);
      };
    }()));
  };
};

exports.deleteTagAssociations = deleteTagAssociations;