"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fetchAppointmentsListRequestSaga = fetchAppointmentsListRequestSaga;
exports.fetchAppointmentsOverviewRequestSaga = fetchAppointmentsOverviewRequestSaga;
exports.fetchPastAppointmentsRequestSaga = fetchPastAppointmentsRequestSaga;
exports.fetchTodayAppointmentsRequestSaga = fetchTodayAppointmentsRequestSaga;
exports.fetchUpcomingAppointmentsRequestSaga = fetchUpcomingAppointmentsRequestSaga;
exports.setAppointmentsListDateSaga = setAppointmentsListDateSaga;
exports.watchAppointments = watchAppointments;
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/regeneratorRuntime"));
var _effects = require("redux-saga/effects");
var _react = require("@kineticdata/react");
var _appointments = require("../modules/appointments");
var _bundleCommon = require("@kineticdata/bundle-common");
var _constants = require("../../constants");
var _moment = _interopRequireDefault(require("moment"));
var _isarray = _interopRequireDefault(require("isarray"));
var _marked = /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(fetchUpcomingAppointmentsRequestSaga),
  _marked2 = /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(fetchPastAppointmentsRequestSaga),
  _marked3 = /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(watchAppointments);
function fetchUpcomingAppointmentsRequestSaga() {
  var kappSlug, username, searchBuilder, _yield$call, submissions, error;
  return (0, _regeneratorRuntime2["default"])().wrap(function fetchUpcomingAppointmentsRequestSaga$(_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;
        _context.next = 5;
        return (0, _effects.select)(function (state) {
          return state.app.profile.username;
        });
      case 5:
        username = _context.sent;
        searchBuilder = new _react.SubmissionSearch().limit(1000).include('details,values').coreState('Submitted').eq('values[Requested For]', username);
        _context.next = 9;
        return (0, _effects.call)(_react.searchSubmissions, {
          search: searchBuilder.build(),
          form: _constants.APPOINTMENT_FORM_SLUG,
          kapp: kappSlug
        });
      case 9:
        _yield$call = _context.sent;
        submissions = _yield$call.submissions;
        error = _yield$call.error;
        if (!error) {
          _context.next = 18;
          break;
        }
        _context.next = 15;
        return (0, _effects.put)(_appointments.actions.fetchAppointmentsFailure(error));
      case 15:
        (0, _bundleCommon.addToastAlert)({
          title: 'Failed to fetch upcoming appointments.',
          message: error.message
        });
        _context.next = 20;
        break;
      case 18:
        _context.next = 20;
        return (0, _effects.put)(_appointments.actions.fetchUpcomingAppointmentsSuccess(submissions));
      case 20:
      case "end":
        return _context.stop();
    }
  }, _marked);
}
function fetchPastAppointmentsRequestSaga() {
  var kappSlug, username, searchBuilder, _yield$call2, submissions, error;
  return (0, _regeneratorRuntime2["default"])().wrap(function fetchPastAppointmentsRequestSaga$(_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.select)(function (state) {
          return state.app.profile.username;
        });
      case 5:
        username = _context2.sent;
        searchBuilder = new _react.SubmissionSearch().limit(1000).include('details,values').coreState('Closed').eq('values[Requested For]', username);
        _context2.next = 9;
        return (0, _effects.call)(_react.searchSubmissions, {
          search: searchBuilder.build(),
          form: _constants.APPOINTMENT_FORM_SLUG,
          kapp: kappSlug
        });
      case 9:
        _yield$call2 = _context2.sent;
        submissions = _yield$call2.submissions;
        error = _yield$call2.error;
        if (!error) {
          _context2.next = 18;
          break;
        }
        _context2.next = 15;
        return (0, _effects.put)(_appointments.actions.fetchAppointmentsFailure(error));
      case 15:
        (0, _bundleCommon.addToastAlert)({
          title: 'Failed to fetch past appointments.',
          message: error.message
        });
        _context2.next = 20;
        break;
      case 18:
        _context2.next = 20;
        return (0, _effects.put)(_appointments.actions.fetchPastAppointmentsSuccess(submissions));
      case 20:
      case "end":
        return _context2.stop();
    }
  }, _marked2);
}
function fetchTodayAppointmentsRequestSaga(_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$call3, submissions, error;
    return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 2;
          return (0, _effects.select)(function (state) {
            return state.app.kappSlug;
          });
        case 2:
          kappSlug = _context3.sent;
          searchBuilder = new _react.SubmissionSearch().limit(1000).include('details,values').eq('values[Scheduler Id]', schedulerId).eq('values[Event 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);
          }
          _context3.next = 7;
          return (0, _effects.call)(_react.searchSubmissions, {
            search: searchBuilder.build(),
            form: _constants.APPOINTMENT_FORM_SLUG,
            kapp: kappSlug
          });
        case 7:
          _yield$call3 = _context3.sent;
          submissions = _yield$call3.submissions;
          error = _yield$call3.error;
          if (!error) {
            _context3.next = 16;
            break;
          }
          _context3.next = 13;
          return (0, _effects.put)(_appointments.actions.fetchAppointmentsFailure(error));
        case 13:
          (0, _bundleCommon.addToastAlert)({
            title: "Failed to fetch today's appointments.",
            message: error.message
          });
          _context3.next = 18;
          break;
        case 16:
          _context3.next = 18;
          return (0, _effects.put)(_appointments.actions.fetchTodayAppointmentsSuccess(submissions.filter(function (s) {
            return s.coreState !== 'Draft';
          })));
        case 18:
        case "end":
          return _context3.stop();
      }
    }, _callee);
  })();
}
function setAppointmentsListDateSaga(_ref2) {
  var payload = _ref2.payload;
  return /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee2() {
    return (0, _regeneratorRuntime2["default"])().wrap(function _callee2$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          _context4.next = 2;
          return (0, _effects.put)(_appointments.actions.fetchAppointmentsListRequest(payload));
        case 2:
        case "end":
          return _context4.stop();
      }
    }, _callee2);
  })();
}
function fetchAppointmentsListRequestSaga(_ref3) {
  var schedulerId = _ref3.payload.schedulerId;
  return /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee3() {
    var kappSlug, date, searchBuilder, _yield$call4, submissions, error;
    return (0, _regeneratorRuntime2["default"])().wrap(function _callee3$(_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          _context5.next = 2;
          return (0, _effects.select)(function (state) {
            return state.app.kappSlug;
          });
        case 2:
          kappSlug = _context5.sent;
          _context5.next = 5;
          return (0, _effects.select)(function (state) {
            return state.appointments.listDate;
          });
        case 5:
          date = _context5.sent;
          searchBuilder = new _react.SubmissionSearch().limit(1000).include('details,values').eq('values[Scheduler Id]', schedulerId).eq('values[Event Date]', date.format('YYYY-MM-DD'));
          _context5.next = 9;
          return (0, _effects.call)(_react.searchSubmissions, {
            search: searchBuilder.build(),
            form: _constants.APPOINTMENT_FORM_SLUG,
            kapp: kappSlug
          });
        case 9:
          _yield$call4 = _context5.sent;
          submissions = _yield$call4.submissions;
          error = _yield$call4.error;
          if (!error) {
            _context5.next = 18;
            break;
          }
          _context5.next = 15;
          return (0, _effects.put)(_appointments.actions.fetchAppointmentsFailure(error));
        case 15:
          (0, _bundleCommon.addToastAlert)({
            title: 'Failed to fetch appointments.',
            message: error.message
          });
          _context5.next = 20;
          break;
        case 18:
          _context5.next = 20;
          return (0, _effects.put)(_appointments.actions.fetchAppointmentsListSuccess(submissions));
        case 20:
        case "end":
          return _context5.stop();
      }
    }, _callee3);
  })();
}
function fetchAppointmentsOverviewRequestSaga(_ref4) {
  var id = _ref4.payload.id;
  return /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee4() {
    var kappSlug, _yield$call5, records, error;
    return (0, _regeneratorRuntime2["default"])().wrap(function _callee4$(_context6) {
      while (1) switch (_context6.prev = _context6.next) {
        case 0:
          _context6.next = 2;
          return (0, _effects.select)(function (state) {
            return state.app.kappSlug;
          });
        case 2:
          kappSlug = _context6.sent;
          _context6.next = 5;
          return (0, _effects.call)(_react.fetchBridgedResource, {
            kappSlug: kappSlug,
            formSlug: 'bridged-resources',
            bridgedResourceName: 'Tech Bar Appointments Overview',
            values: {
              Id: id,
              Date: (0, _moment["default"])().format('YYYY-MM-DD')
            }
          });
        case 5:
          _yield$call5 = _context6.sent;
          records = _yield$call5.records;
          error = _yield$call5.error;
          if (!error) {
            _context6.next = 14;
            break;
          }
          _context6.next = 11;
          return (0, _effects.put)(_appointments.actions.fetchAppointmentsOverviewFailure(error));
        case 11:
          console.error('Failed to fetch appointments overview.', error.message);
          _context6.next = 16;
          break;
        case 14:
          _context6.next = 16;
          return (0, _effects.put)(_appointments.actions.fetchAppointmentsOverviewSuccess(records));
        case 16:
        case "end":
          return _context6.stop();
      }
    }, _callee4);
  })();
}
function watchAppointments() {
  return (0, _regeneratorRuntime2["default"])().wrap(function watchAppointments$(_context7) {
    while (1) switch (_context7.prev = _context7.next) {
      case 0:
        _context7.next = 2;
        return (0, _effects.takeEvery)(_appointments.types.FETCH_UPCOMING_APPOINTMENTS_REQUEST, fetchUpcomingAppointmentsRequestSaga);
      case 2:
        _context7.next = 4;
        return (0, _effects.takeEvery)(_appointments.types.FETCH_PAST_APPOINTMENTS_REQUEST, fetchPastAppointmentsRequestSaga);
      case 4:
        _context7.next = 6;
        return (0, _effects.takeEvery)(_appointments.types.FETCH_TODAY_APPOINTMENTS_REQUEST, fetchTodayAppointmentsRequestSaga);
      case 6:
        _context7.next = 8;
        return (0, _effects.takeEvery)(_appointments.types.SET_APPOINTMENTS_LIST_DATE, setAppointmentsListDateSaga);
      case 8:
        _context7.next = 10;
        return (0, _effects.takeEvery)(_appointments.types.FETCH_APPOINTMENTS_LIST_REQUEST, fetchAppointmentsListRequestSaga);
      case 10:
        _context7.next = 12;
        return (0, _effects.takeEvery)(_appointments.types.FETCH_APPOINTMENTS_OVERVIEW_REQUEST, fetchAppointmentsOverviewRequestSaga);
      case 12:
      case "end":
        return _context7.stop();
    }
  }, _marked3);
}