"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.fetchTodayWalkInsRequestSaga = fetchTodayWalkInsRequestSaga;
exports.fetchWalkInsOverviewRequestSaga = fetchWalkInsOverviewRequestSaga;
exports.watchWalkIns = watchWalkIns;
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/regeneratorRuntime"));
var _effects = require("redux-saga/effects");
var _react = require("@kineticdata/react");
var _walkIns = require("../modules/walkIns");
var _bundleCommon = require("@kineticdata/bundle-common");
var _constants = require("../../constants");
var _moment = _interopRequireDefault(require("moment"));
var _isarray = _interopRequireDefault(require("isarray"));
var _marked = /*#__PURE__*/_regeneratorRuntime3().mark(watchWalkIns);
function fetchTodayWalkInsRequestSaga(_ref) {
  var _ref$payload = _ref.payload,
    schedulerId = _ref$payload.schedulerId,
    status = _ref$payload.status;
  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').eq('values[Scheduler Id]', schedulerId).eq('values[Date]', (0, _moment["default"])().format('YYYY-MM-DD'));
          if ((0, _isarray["default"])(status) && status.length > 0) {
            searchBuilder["in"]('values[Status]', status);
          } else if (status) {
            searchBuilder.eq('values[Status]', status);
          }
          _context.next = 7;
          return (0, _effects.call)(_react.searchSubmissions, {
            search: searchBuilder.build(),
            form: _constants.WALK_IN_FORM_SLUG,
            kapp: kappSlug
          });
        case 7:
          _yield$call = _context.sent;
          submissions = _yield$call.submissions;
          error = _yield$call.error;
          if (!error) {
            _context.next = 16;
            break;
          }
          _context.next = 13;
          return (0, _effects.put)(_walkIns.actions.fetchWalkInsFailure(error));
        case 13:
          (0, _bundleCommon.addToastAlert)({
            title: "Failed to fetch today's walk-ins.",
            message: error.message
          });
          _context.next = 18;
          break;
        case 16:
          _context.next = 18;
          return (0, _effects.put)(_walkIns.actions.fetchTodayWalkInsSuccess(submissions.filter(function (s) {
            return s.coreState !== 'Draft';
          })));
        case 18:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })();
}
function fetchWalkInsOverviewRequestSaga(_ref2) {
  var id = _ref2.payload.id;
  return /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee2() {
    var kappSlug, _yield$call2, records, error;
    return (0, _regeneratorRuntime2["default"])().wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return (0, _effects.select)(function (state) {
            return state.app.kappSlug;
          });
        case 2:
          kappSlug = _context2.sent;
          _context2.next = 5;
          return (0, _effects.call)(_react.fetchBridgedResource, {
            kappSlug: kappSlug,
            formSlug: 'bridged-resources',
            bridgedResourceName: 'Tech Bar Walk-Ins Overview',
            values: {
              Id: id,
              Date: (0, _moment["default"])().format('YYYY-MM-DD')
            }
          });
        case 5:
          _yield$call2 = _context2.sent;
          records = _yield$call2.records;
          error = _yield$call2.error;
          if (!error) {
            _context2.next = 14;
            break;
          }
          _context2.next = 11;
          return (0, _effects.put)(_walkIns.actions.fetchWalkInsOverviewFailure(error));
        case 11:
          console.error('Failed to fetch walk-ins overview.', error.message);
          _context2.next = 16;
          break;
        case 14:
          _context2.next = 16;
          return (0, _effects.put)(_walkIns.actions.fetchWalkInsOverviewSuccess(records));
        case 16:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  })();
}
function watchWalkIns() {
  return (0, _regeneratorRuntime2["default"])().wrap(function watchWalkIns$(_context3) {
    while (1) switch (_context3.prev = _context3.next) {
      case 0:
        _context3.next = 2;
        return (0, _effects.takeEvery)(_walkIns.types.FETCH_TODAY_WALK_INS_REQUEST, fetchTodayWalkInsRequestSaga);
      case 2:
        _context3.next = 4;
        return (0, _effects.takeEvery)(_walkIns.types.FETCH_WALK_INS_OVERVIEW_REQUEST, fetchWalkInsOverviewRequestSaga);
      case 4:
      case "end":
        return _context3.stop();
    }
  }, _marked);
}