'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getItem = getItem;
exports.createItem = createItem;
exports.updateItem = updateItem;

var _first = require('lodash/first');

var _first2 = _interopRequireDefault(_first);

var _constants = require('../../constants');

var _helpers = require('../../banks/api/helpers');

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

var _Fetcher2 = _interopRequireDefault(_Fetcher);

var _callHandlers = require('../../common/api/callHandlers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// =====================
// =====================
//       CREATE
// =====================
// =====================

function getItem(itemId) {
  var url = '/api/items/' + itemId;
  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default();

    return fetcher.get(url).then(function (itemData) {
      dispatch((0, _callHandlers.handleItemsResponse)([itemData]));
      return itemData;
    });
  };
} // =========================================
// =========================================

//  this file is in charge of coordinating
//  the dispatching of actions necessary
//  to interact with items via the api

// =========================================
// =========================================

function createItem(item, parentId, parentType) {
  var parentItemId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

  var resource = parentType === 'bank' ? 'banks' : 'quizzes';
  var url = '/api/' + resource + '/' + parentId + '/items';
  var itemParams = {
    feedback: item.feedback,
    interaction_data: item.interactionData,
    interaction_type_id: item.interactionType,
    item_body: item.itemBody,
    outcome_alignment_set_guid: item.outcomeAlignmentSetGuid,
    parent_item_id: parentItemId,
    properties: item.properties,
    scoring_algorithm: item.getScoringAlgorithm(),
    scoring_data: item.scoringData,
    title: item.title,
    user_response_type: (0, _first2.default)(item.getInteractionType().userResponseTypeOptions),
    stimulus_id: item.stimulusId
  };
  var requestBody = JSON.stringify({ item: itemParams });

  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default({
      callType: _constants.CREATE_ITEM_CALL,
      onError: parentType === 'bank' && (0, _helpers.handleBankError)(dispatch)
    });

    var createItemParams = {
      body: requestBody
    };

    return fetcher.post(url, createItemParams).then(function (itemData) {
      dispatch((0, _callHandlers.handleItemsResponse)([itemData]));
      return itemData;
    });
  };
}

// =====================
// =====================
//       UPDATE
// =====================
// =====================

function updateItem(item, parentId, parentType) {
  var resource = parentType === 'bank' ? 'banks' : 'quizzes';
  var url = '/api/' + resource + '/' + parentId + '/items/' + item.id;
  var itemData = {
    item_body: item.itemBody,
    title: item.title,
    scoring_data: item.scoringData,
    scoring_algorithm: item.getScoringAlgorithm(),
    interaction_data: item.interactionData,
    properties: item.properties,
    outcome_alignment_set_guid: item.outcomeAlignmentSetGuid,
    feedback: item.feedback
  };

  var reqBody = JSON.stringify({ item: itemData });
  return function (dispatch, getState) {
    var updateItemParams = {
      body: reqBody
    };

    var fetcher = new _Fetcher2.default({
      callType: _constants.UPDATE_ITEM_CALL,
      onError: parentType === 'bank' && (0, _helpers.handleBankError)(dispatch)
    });

    return fetcher.patch(url, updateItemParams).then(function (itemData) {
      dispatch((0, _callHandlers.handleItemsResponse)([itemData]));
      return itemData;
    });
  };
}