"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.types = exports.reducer = exports.actions = exports.State = void 0;
var _immutable = require("immutable");
var _bundleCommon = require("@kineticdata/bundle-common");
var _moment = _interopRequireDefault(require("moment"));
var noPayload = _bundleCommon.Utils.noPayload,
  withPayload = _bundleCommon.Utils.withPayload;
var ns = _bundleCommon.Utils.namespaceBuilder('techbar/appointments');
var types = exports.types = {
  FETCH_APPOINTMENTS_FAILURE: ns('FETCH_APPOINTMENTS_FAILURE'),
  FETCH_UPCOMING_APPOINTMENTS_REQUEST: ns('FETCH_UPCOMING_APPOINTMENTS_REQUEST'),
  FETCH_UPCOMING_APPOINTMENTS_SUCCESS: ns('FETCH_UPCOMING_APPOINTMENTS_SUCCESS'),
  FETCH_PAST_APPOINTMENTS_REQUEST: ns('FETCH_PAST_APPOINTMENTS_REQUEST'),
  FETCH_PAST_APPOINTMENTS_SUCCESS: ns('FETCH_PAST_APPOINTMENTS_SUCCESS'),
  FETCH_TODAY_APPOINTMENTS_REQUEST: ns('FETCH_TODAY_APPOINTMENTS_REQUEST'),
  FETCH_TODAY_APPOINTMENTS_SUCCESS: ns('FETCH_TODAY_APPOINTMENTS_SUCCESS'),
  SET_APPOINTMENTS_LIST_DATE: ns('SET_APPOINTMENTS_LIST_DATE'),
  FETCH_APPOINTMENTS_LIST_REQUEST: ns('FETCH_APPOINTMENTS_LIST_REQUEST'),
  FETCH_APPOINTMENTS_LIST_SUCCESS: ns('FETCH_APPOINTMENTS_LIST_SUCCESS'),
  FETCH_APPOINTMENTS_OVERVIEW_REQUEST: ns('FETCH_APPOINTMENTS_OVERVIEW_REQUEST'),
  FETCH_APPOINTMENTS_OVERVIEW_SUCCESS: ns('FETCH_APPOINTMENTS_OVERVIEW_SUCCESS'),
  FETCH_APPOINTMENTS_OVERVIEW_FAILURE: ns('FETCH_APPOINTMENTS_OVERVIEW_FAILURE')
};
var actions = exports.actions = {
  fetchAppointmentsFailure: withPayload(types.FETCH_APPOINTMENTS_FAILURE),
  fetchUpcomingAppointmentsRequest: noPayload(types.FETCH_UPCOMING_APPOINTMENTS_REQUEST),
  fetchUpcomingAppointmentsSuccess: withPayload(types.FETCH_UPCOMING_APPOINTMENTS_SUCCESS),
  fetchPastAppointmentsRequest: noPayload(types.FETCH_PAST_APPOINTMENTS_REQUEST),
  fetchPastAppointmentsSuccess: withPayload(types.FETCH_PAST_APPOINTMENTS_SUCCESS),
  fetchTodayAppointmentsRequest: withPayload(types.FETCH_TODAY_APPOINTMENTS_REQUEST),
  fetchTodayAppointmentsSuccess: withPayload(types.FETCH_TODAY_APPOINTMENTS_SUCCESS),
  setAppointmentsListDate: withPayload(types.SET_APPOINTMENTS_LIST_DATE),
  fetchAppointmentsListRequest: withPayload(types.FETCH_APPOINTMENTS_LIST_REQUEST),
  fetchAppointmentsListSuccess: withPayload(types.FETCH_APPOINTMENTS_LIST_SUCCESS),
  fetchAppointmentsOverviewRequest: withPayload(types.FETCH_APPOINTMENTS_OVERVIEW_REQUEST),
  fetchAppointmentsOverviewSuccess: withPayload(types.FETCH_APPOINTMENTS_OVERVIEW_SUCCESS),
  fetchAppointmentsOverviewFailure: withPayload(types.FETCH_APPOINTMENTS_OVERVIEW_FAILURE)
};
var State = exports.State = (0, _immutable.Record)({
  loading: true,
  error: null,
  upcoming: null,
  past: null,
  today: null,
  listDate: (0, _moment["default"])(),
  list: null,
  overview: null,
  overviewError: null
});
var reducer = exports.reducer = function reducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : State();
  var _ref = arguments.length > 1 ? arguments[1] : undefined,
    type = _ref.type,
    payload = _ref.payload;
  switch (type) {
    case types.FETCH_APPOINTMENTS_FAILURE:
      return state.set('error', payload);
    case types.FETCH_UPCOMING_APPOINTMENTS_REQUEST:
      return state.set('error', null).set('upcoming', null);
    case types.FETCH_UPCOMING_APPOINTMENTS_SUCCESS:
      return state.set('upcoming', (0, _immutable.List)(payload).sortBy(function (a) {
        return "".concat(a.values['Event Date'], " ").concat(a.values['Event Time']);
      }));
    case types.FETCH_PAST_APPOINTMENTS_REQUEST:
      return state.set('error', null).set('past', null);
    case types.FETCH_PAST_APPOINTMENTS_SUCCESS:
      return state.set('past', (0, _immutable.List)(payload).sortBy(function (a) {
        return "".concat(a.values['Event Date'], " ").concat(a.values['Event Time']);
      }, function (a, b) {
        return a < b ? 1 : a > b ? -1 : 0;
      }));
    case types.FETCH_TODAY_APPOINTMENTS_REQUEST:
      return state.set('error', null).set('today', null);
    case types.FETCH_TODAY_APPOINTMENTS_SUCCESS:
      return state.set('today', (0, _immutable.List)(payload).sortBy(function (a) {
        return "".concat(a.values['Event Date'], " ").concat(a.values['Event Time']);
      }));
    case types.SET_APPOINTMENTS_LIST_DATE:
      return state.set('listDate', (0, _moment["default"])(payload.date));
    case types.FETCH_APPOINTMENTS_LIST_REQUEST:
      return state.set('error', null).set('list', null);
    case types.FETCH_APPOINTMENTS_LIST_SUCCESS:
      return state.set('list', (0, _immutable.List)(payload).sortBy(function (a) {
        return "".concat(a.values['Event Date'], " ").concat(a.values['Event Time']);
      }));
    case types.FETCH_APPOINTMENTS_OVERVIEW_REQUEST:
      return state.set('overviewError', null).set('overview', null);
    case types.FETCH_APPOINTMENTS_OVERVIEW_SUCCESS:
      return state.set('overview', (0, _immutable.List)(payload));
    case types.FETCH_APPOINTMENTS_OVERVIEW_FAILURE:
      return state.set('overviewError', payload);
    default:
      return state;
  }
};