"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
var _regeneratorRuntime3 = require("@babel/runtime/helpers/regeneratorRuntime")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fetchMetricsRequestSaga = fetchMetricsRequestSaga;
exports.watchMetrics = watchMetrics;
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/regeneratorRuntime"));
var _effects = require("redux-saga/effects");
var _react = require("@kineticdata/react");
var _bundleCommon = require("@kineticdata/bundle-common");
var _metrics = require("../modules/metrics");
var _constants = require("../../constants");
var _isarray = _interopRequireDefault(require("isarray"));
var _marked = /*#__PURE__*/_regeneratorRuntime3().mark(watchMetrics);
function fetchMetricsRequestSaga(_ref) {
  var _ref$payload = _ref.payload,
    schedulerIds = _ref$payload.schedulerIds,
    _ref$payload$monthly = _ref$payload.monthly,
    monthly = _ref$payload$monthly === void 0 ? false : _ref$payload$monthly,
    dates = _ref$payload.dates;
  return /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee() {
    var kappSlug, searchBuilder, _yield$call, submissions, error;
    return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return (0, _effects.select)(function (state) {
            return state.app.kappSlug;
          });
        case 2:
          kappSlug = _context.sent;
          searchBuilder = new _react.SubmissionSearch().limit(1000).include('details,values').coreState('Submitted').eq('values[Period Type]', monthly ? 'Monthly' : 'Daily');
          if ((0, _isarray["default"])(schedulerIds) && schedulerIds.length > 0) {
            searchBuilder["in"]('values[Scheduler Id]', schedulerIds);
          }
          if ((0, _isarray["default"])(dates) && dates.length > 0) {
            searchBuilder["in"]('values[Period]', dates);
          }
          _context.next = 8;
          return (0, _effects.call)(_react.searchSubmissions, {
            search: searchBuilder.build(),
            form: _constants.METRICS_FORM_SLUG,
            kapp: kappSlug
          });
        case 8:
          _yield$call = _context.sent;
          submissions = _yield$call.submissions;
          error = _yield$call.error;
          if (!error) {
            _context.next = 17;
            break;
          }
          (0, _bundleCommon.addToastAlert)({
            title: 'Error retrieving metrics data.',
            message: error.message
          });
          _context.next = 15;
          return (0, _effects.put)(_metrics.actions.fetchMetricsFailure(error));
        case 15:
          _context.next = 19;
          break;
        case 17:
          _context.next = 19;
          return (0, _effects.put)(_metrics.actions.fetchMetricsSuccess(submissions));
        case 19:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })();
}
function watchMetrics() {
  return (0, _regeneratorRuntime2["default"])().wrap(function watchMetrics$(_context2) {
    while (1) switch (_context2.prev = _context2.next) {
      case 0:
        _context2.next = 2;
        return (0, _effects.takeEvery)(_metrics.types.FETCH_METRICS_REQUEST, fetchMetricsRequestSaga);
      case 2:
      case "end":
        return _context2.stop();
    }
  }, _marked);
}