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

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(fetchTodayWalkInsRequestSaga),
    _marked2 = /*#__PURE__*/_regeneratorRuntime.mark(fetchWalkInsOverviewRequestSaga),
    _marked3 = /*#__PURE__*/_regeneratorRuntime.mark(watchWalkIns);

import { takeEvery, put, call, select } from 'redux-saga/effects';
import { fetchBridgedResource, searchSubmissions, SubmissionSearch } from '@kineticdata/react';
import { actions, types } from '../modules/walkIns';
import { addToastAlert } from '@kineticdata/bundle-common';
import { WALK_IN_FORM_SLUG } from '../../constants';
import moment from 'moment';
import isarray from 'isarray';
export function fetchTodayWalkInsRequestSaga(_ref) {
  var _ref$payload, schedulerId, status, kappSlug, searchBuilder, _yield$call, submissions, error;

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

        case 3:
          kappSlug = _context.sent;
          searchBuilder = new SubmissionSearch().limit(1000).include('details,values').eq('values[Scheduler Id]', schedulerId).eq('values[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);
          }

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

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

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

          _context.next = 14;
          return put(actions.fetchWalkInsFailure(error));

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

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

        case 19:
        case "end":
          return _context.stop();
      }
    }
  }, _marked);
}
export function fetchWalkInsOverviewRequestSaga(_ref2) {
  var id, kappSlug, _yield$call2, records, error;

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

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

        case 6:
          _yield$call2 = _context2.sent;
          records = _yield$call2.records;
          error = _yield$call2.error;

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

          _context2.next = 12;
          return put(actions.fetchWalkInsOverviewFailure(error));

        case 12:
          console.error('Failed to fetch walk-ins overview.', error.message);
          _context2.next = 17;
          break;

        case 15:
          _context2.next = 17;
          return put(actions.fetchWalkInsOverviewSuccess(records));

        case 17:
        case "end":
          return _context2.stop();
      }
    }
  }, _marked2);
}
export function watchWalkIns() {
  return _regeneratorRuntime.wrap(function watchWalkIns$(_context3) {
    while (1) {
      switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 2;
          return takeEvery(types.FETCH_TODAY_WALK_INS_REQUEST, fetchTodayWalkInsRequestSaga);

        case 2:
          _context3.next = 4;
          return takeEvery(types.FETCH_WALK_INS_OVERVIEW_REQUEST, fetchWalkInsOverviewRequestSaga);

        case 4:
        case "end":
          return _context3.stop();
      }
    }
  }, _marked3);
}