"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.callProgress = callProgress;
exports.progressValue = progressValue;
exports.default = exports.generateImportModalId = void 0;

var _immutable = require("immutable");

var _reactRedux = require("../../react-redux");

var _modal = require("../../../common/actions/modal");

var _upload = require("../../../common/actions/upload");

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

var _presenter = _interopRequireDefault(require("./presenter"));

var callsToCheck = [_constants.FILE_UPLOAD_URL_CALL, _constants.FILE_UPLOAD_CALL];

function callProgress(callsToCheck) {
  var calls = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : (0, _immutable.Map)();
  var successCount = calls.filter(function (value, key) {
    return callsToCheck.includes(key) && value.get('requestStatus') === 'success';
  }).size;
  return successCount / callsToCheck.length;
}

function progressValue(progressNumber) {
  var callsMade = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : (0, _immutable.Map)();
  var callsToCheck = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [];
  var callProgressWhenCompleted = 1; // We are using 50 because 50 == (100 / 2.0) and import and API calls each contribute 1/2 of the overall progress

  return progressNumber // If importing, then the calls are completed and we skip the calculation
  ? (callProgressWhenCompleted + progressNumber) * 50 // If no progressNumber, then the callProgress counts is working on the first 50%
  : callProgress(callsToCheck, callsMade) * 50;
}

var generateImportModalId = function generateImportModalId(id) {
  return "".concat(id, "_").concat(_constants.IMPORT_MODAL);
}; // Every component ought to pass in the importModalId to help keep track of which importModal is supposed to be open


exports.generateImportModalId = generateImportModalId;

var mapStateToProps = function mapStateToProps(state, props) {
  return {
    altText: state.getIn(['upload', 'altText'], ''),
    modalOpen: state.getIn(['modal', 'isOpen']) === generateImportModalId(props.importModalId),
    files: state.getIn(['upload', 'files'], (0, _immutable.List)()),
    uploadStatus: state.getIn(['upload', 'status'], _constants.FILE_UPLOAD_SELECT),
    progressValue: callProgress(callsToCheck, state.get('calls')) * 100
  };
};

var mapDispatchToProps = {
  clearAltText: _upload.clearAltText,
  clearFiles: _upload.clearFiles,
  closeModal: _modal.closeModal,
  receiveFile: _upload.receiveFile,
  setAltText: _upload.setAltText,
  setUploadStatus: _upload.uploadStatus
};

var mergeProps = function mergeProps(stateProps, dispatchProps, ownProps) {
  return Object.assign({}, stateProps, dispatchProps, ownProps);
};

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(_presenter.default);

exports.default = _default;