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

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(fetchUpcomingAppointmentsRequestSaga),
    _marked2 = /*#__PURE__*/_regeneratorRuntime.mark(fetchPastAppointmentsRequestSaga),
    _marked3 = /*#__PURE__*/_regeneratorRuntime.mark(fetchTodayAppointmentsRequestSaga),
    _marked4 = /*#__PURE__*/_regeneratorRuntime.mark(setAppointmentsListDateSaga),
    _marked5 = /*#__PURE__*/_regeneratorRuntime.mark(fetchAppointmentsListRequestSaga),
    _marked6 = /*#__PURE__*/_regeneratorRuntime.mark(fetchAppointmentsOverviewRequestSaga),
    _marked7 = /*#__PURE__*/_regeneratorRuntime.mark(watchAppointments);

import { takeEvery, put, call, select } from 'redux-saga/effects';
import { fetchBridgedResource, searchSubmissions, SubmissionSearch } from '@kineticdata/react';
import { actions, types } from '../modules/appointments';
import { addToastAlert } from '@kineticdata/bundle-common';
import { APPOINTMENT_FORM_SLUG } from '../../constants';
import moment from 'moment';
import isarray from 'isarray';
export function fetchUpcomingAppointmentsRequestSaga() {
  var kappSlug, username, searchBuilder, _yield$call, submissions, error;

  return _regeneratorRuntime.wrap(function fetchUpcomingAppointmentsRequestSaga$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return select(function (state) {
            return state.app.kappSlug;
          });

        case 2:
          kappSlug = _context.sent;
          _context.next = 5;
          return select(function (state) {
            return state.app.profile.username;
          });

        case 5:
          username = _context.sent;
          searchBuilder = new SubmissionSearch().limit(1000).include('details,values').coreState('Submitted').eq('values[Requested For]', username);
          _context.next = 9;
          return call(searchSubmissions, {
            search: searchBuilder.build(),
            form: 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 put(actions.fetchAppointmentsFailure(error));

        case 15:
          addToastAlert({
            title: 'Failed to fetch upcoming appointments.',
            message: error.message
          });
          _context.next = 20;
          break;

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

        case 20:
        case "end":
          return _context.stop();
      }
    }
  }, _marked);
}
export function fetchPastAppointmentsRequestSaga() {
  var kappSlug, username, searchBuilder, _yield$call2, submissions, error;

  return _regeneratorRuntime.wrap(function fetchPastAppointmentsRequestSaga$(_context2) {
    while (1) {
      switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return select(function (state) {
            return state.app.kappSlug;
          });

        case 2:
          kappSlug = _context2.sent;
          _context2.next = 5;
          return select(function (state) {
            return state.app.profile.username;
          });

        case 5:
          username = _context2.sent;
          searchBuilder = new SubmissionSearch().limit(1000).include('details,values').coreState('Closed').eq('values[Requested For]', username);
          _context2.next = 9;
          return call(searchSubmissions, {
            search: searchBuilder.build(),
            form: 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 put(actions.fetchAppointmentsFailure(error));

        case 15:
          addToastAlert({
            title: 'Failed to fetch past appointments.',
            message: error.message
          });
          _context2.next = 20;
          break;

        case 18:
          _context2.next = 20;
          return put(actions.fetchPastAppointmentsSuccess(submissions));

        case 20:
        case "end":
          return _context2.stop();
      }
    }
  }, _marked2);
}
export function fetchTodayAppointmentsRequestSaga(_ref) {
  var _ref$payload, schedulerId, status, kappSlug, searchBuilder, _yield$call3, submissions, error;

  return _regeneratorRuntime.wrap(function fetchTodayAppointmentsRequestSaga$(_context3) {
    while (1) {
      switch (_context3.prev = _context3.next) {
        case 0:
          _ref$payload = _ref.payload, schedulerId = _ref$payload.schedulerId, status = _ref$payload.status;
          _context3.next = 3;
          return select(function (state) {
            return state.app.kappSlug;
          });

        case 3:
          kappSlug = _context3.sent;
          searchBuilder = new SubmissionSearch().limit(1000).include('details,values').eq('values[Scheduler Id]', schedulerId).eq('values[Event Date]', moment().format('YYYY-MM-DD'));

          if (isarray(status) && status.length > 0) {
            searchBuilder["in"]('values[Status]', status);
          } else if (status) {
            searchBuilder.eq('values[Status]', status);
          }

          _context3.next = 8;
          return call(searchSubmissions, {
            search: searchBuilder.build(),
            form: APPOINTMENT_FORM_SLUG,
            kapp: kappSlug
          });

        case 8:
          _yield$call3 = _context3.sent;
          submissions = _yield$call3.submissions;
          error = _yield$call3.error;

          if (!error) {
            _context3.next = 17;
            break;
          }

          _context3.next = 14;
          return put(actions.fetchAppointmentsFailure(error));

        case 14:
          addToastAlert({
            title: "Failed to fetch today's appointments.",
            message: error.message
          });
          _context3.next = 19;
          break;

        case 17:
          _context3.next = 19;
          return put(actions.fetchTodayAppointmentsSuccess(submissions.filter(function (s) {
            return s.coreState !== 'Draft';
          })));

        case 19:
        case "end":
          return _context3.stop();
      }
    }
  }, _marked3);
}
export function setAppointmentsListDateSaga(_ref2) {
  var payload;
  return _regeneratorRuntime.wrap(function setAppointmentsListDateSaga$(_context4) {
    while (1) {
      switch (_context4.prev = _context4.next) {
        case 0:
          payload = _ref2.payload;
          _context4.next = 3;
          return put(actions.fetchAppointmentsListRequest(payload));

        case 3:
        case "end":
          return _context4.stop();
      }
    }
  }, _marked4);
}
export function fetchAppointmentsListRequestSaga(_ref3) {
  var schedulerId, kappSlug, date, searchBuilder, _yield$call4, submissions, error;

  return _regeneratorRuntime.wrap(function fetchAppointmentsListRequestSaga$(_context5) {
    while (1) {
      switch (_context5.prev = _context5.next) {
        case 0:
          schedulerId = _ref3.payload.schedulerId;
          _context5.next = 3;
          return select(function (state) {
            return state.app.kappSlug;
          });

        case 3:
          kappSlug = _context5.sent;
          _context5.next = 6;
          return select(function (state) {
            return state.appointments.listDate;
          });

        case 6:
          date = _context5.sent;
          searchBuilder = new SubmissionSearch().limit(1000).include('details,values').eq('values[Scheduler Id]', schedulerId).eq('values[Event Date]', date.format('YYYY-MM-DD'));
          _context5.next = 10;
          return call(searchSubmissions, {
            search: searchBuilder.build(),
            form: APPOINTMENT_FORM_SLUG,
            kapp: kappSlug
          });

        case 10:
          _yield$call4 = _context5.sent;
          submissions = _yield$call4.submissions;
          error = _yield$call4.error;

          if (!error) {
            _context5.next = 19;
            break;
          }

          _context5.next = 16;
          return put(actions.fetchAppointmentsFailure(error));

        case 16:
          addToastAlert({
            title: 'Failed to fetch appointments.',
            message: error.message
          });
          _context5.next = 21;
          break;

        case 19:
          _context5.next = 21;
          return put(actions.fetchAppointmentsListSuccess(submissions));

        case 21:
        case "end":
          return _context5.stop();
      }
    }
  }, _marked5);
}
export function fetchAppointmentsOverviewRequestSaga(_ref4) {
  var id, kappSlug, _yield$call5, records, error;

  return _regeneratorRuntime.wrap(function fetchAppointmentsOverviewRequestSaga$(_context6) {
    while (1) {
      switch (_context6.prev = _context6.next) {
        case 0:
          id = _ref4.payload.id;
          _context6.next = 3;
          return select(function (state) {
            return state.app.kappSlug;
          });

        case 3:
          kappSlug = _context6.sent;
          _context6.next = 6;
          return call(fetchBridgedResource, {
            kappSlug: kappSlug,
            formSlug: 'bridged-resources',
            bridgedResourceName: 'Tech Bar Appointments Overview',
            values: {
              Id: id,
              Date: moment().format('YYYY-MM-DD')
            }
          });

        case 6:
          _yield$call5 = _context6.sent;
          records = _yield$call5.records;
          error = _yield$call5.error;

          if (!error) {
            _context6.next = 15;
            break;
          }

          _context6.next = 12;
          return put(actions.fetchAppointmentsOverviewFailure(error));

        case 12:
          console.error('Failed to fetch appointments overview.', error.message);
          _context6.next = 17;
          break;

        case 15:
          _context6.next = 17;
          return put(actions.fetchAppointmentsOverviewSuccess(records));

        case 17:
        case "end":
          return _context6.stop();
      }
    }
  }, _marked6);
}
export function watchAppointments() {
  return _regeneratorRuntime.wrap(function watchAppointments$(_context7) {
    while (1) {
      switch (_context7.prev = _context7.next) {
        case 0:
          _context7.next = 2;
          return takeEvery(types.FETCH_UPCOMING_APPOINTMENTS_REQUEST, fetchUpcomingAppointmentsRequestSaga);

        case 2:
          _context7.next = 4;
          return takeEvery(types.FETCH_PAST_APPOINTMENTS_REQUEST, fetchPastAppointmentsRequestSaga);

        case 4:
          _context7.next = 6;
          return takeEvery(types.FETCH_TODAY_APPOINTMENTS_REQUEST, fetchTodayAppointmentsRequestSaga);

        case 6:
          _context7.next = 8;
          return takeEvery(types.SET_APPOINTMENTS_LIST_DATE, setAppointmentsListDateSaga);

        case 8:
          _context7.next = 10;
          return takeEvery(types.FETCH_APPOINTMENTS_LIST_REQUEST, fetchAppointmentsListRequestSaga);

        case 10:
          _context7.next = 12;
          return takeEvery(types.FETCH_APPOINTMENTS_OVERVIEW_REQUEST, fetchAppointmentsOverviewRequestSaga);

        case 12:
        case "end":
          return _context7.stop();
      }
    }
  }, _marked7);
}