import _regeneratorRuntime from "@babel/runtime/regenerator";

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(fetchMetricsRequestSaga),
    _marked2 = /*#__PURE__*/_regeneratorRuntime.mark(watchMetrics);

import { takeEvery, put, call, select } from 'redux-saga/effects';
import { searchSubmissions, SubmissionSearch } from '@kineticdata/react';
import { addToastAlert } from '@kineticdata/bundle-common';
import { actions, types } from '../modules/metrics';
import { METRICS_FORM_SLUG } from '../../constants';
import isarray from 'isarray';
export function fetchMetricsRequestSaga(_ref) {
  var _ref$payload, schedulerIds, _ref$payload$monthly, monthly, dates, kappSlug, searchBuilder, _yield$call, submissions, error;

  return _regeneratorRuntime.wrap(function fetchMetricsRequestSaga$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _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;
          _context.next = 3;
          return select(function (state) {
            return state.app.kappSlug;
          });

        case 3:
          kappSlug = _context.sent;
          searchBuilder = new SubmissionSearch().limit(1000).include('details,values').coreState('Submitted').eq('values[Period Type]', monthly ? 'Monthly' : 'Daily');

          if (isarray(schedulerIds) && schedulerIds.length > 0) {
            searchBuilder["in"]('values[Scheduler Id]', schedulerIds);
          }

          if (isarray(dates) && dates.length > 0) {
            searchBuilder["in"]('values[Period]', dates);
          }

          _context.next = 9;
          return call(searchSubmissions, {
            search: searchBuilder.build(),
            form: METRICS_FORM_SLUG,
            kapp: kappSlug
          });

        case 9:
          _yield$call = _context.sent;
          submissions = _yield$call.submissions;
          error = _yield$call.error;

          if (!error) {
            _context.next = 18;
            break;
          }

          addToastAlert({
            title: 'Error retrieving metrics data.',
            message: error.message
          });
          _context.next = 16;
          return put(actions.fetchMetricsFailure(error));

        case 16:
          _context.next = 20;
          break;

        case 18:
          _context.next = 20;
          return put(actions.fetchMetricsSuccess(submissions));

        case 20:
        case "end":
          return _context.stop();
      }
    }
  }, _marked);
}
export function watchMetrics() {
  return _regeneratorRuntime.wrap(function watchMetrics$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return takeEvery(types.FETCH_METRICS_REQUEST, fetchMetricsRequestSaga);

        case 2:
        case "end":
          return _context2.stop();
      }
    }
  }, _marked2);
}