"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _immutabilityHelper = _interopRequireDefault(require("immutability-helper"));

var _moment = _interopRequireDefault(require("moment"));

var _callTaker = require("../util/call-taker");

var _constants = require("../util/constants");

var _config = require("../util/config");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function createCallTakerReducer(config) {
  var _calltakerConfig$opti;

  const calltakerConfig = (0, _config.getModuleConfig)({
    otp: {
      config
    }
  }, _config.Modules.CALL_TAKER);
  const initialState = {
    activeCall: null,
    callHistory: {
      calls: {
        data: [],
        status: _constants.FETCH_STATUS.UNFETCHED
      },
      visible: calltakerConfig === null || calltakerConfig === void 0 ? void 0 : (_calltakerConfig$opti = calltakerConfig.options) === null || _calltakerConfig$opti === void 0 ? void 0 : _calltakerConfig$opti.showCallHistoryOnLoad
    },
    fieldTrip: {
      activeId: null,
      filter: {
        tab: 'new'
      },
      groupSize: null,
      requests: {
        data: [],
        status: _constants.FETCH_STATUS.UNFETCHED
      },
      travelDateTripsInUse: [],
      tripHashLookup: {},
      visible: false
    },
    mailables: {
      visible: false
    },
    session: null
  };
  return (state = initialState, action) => {
    switch (action.type) {
      case 'BEGIN_CALL':
        {
          const newCall = (0, _callTaker.constructNewCall)(); // Initialize new call and show call history window.

          return (0, _immutabilityHelper.default)(state, {
            activeCall: {
              $set: newCall
            },
            callHistory: {
              visible: {
                $set: true
              }
            }
          });
        }

      case 'REQUESTING_CALLS':
        {
          return (0, _immutabilityHelper.default)(state, {
            callHistory: {
              calls: {
                status: {
                  $set: _constants.FETCH_STATUS.FETCHING
                }
              }
            }
          });
        }

      case 'RECEIVED_CALLS':
        {
          const data = action.payload.calls;
          const calls = {
            data: data.sort((a, b) => (0, _moment.default)(b.endTime) - (0, _moment.default)(a.endTime)),
            status: _constants.FETCH_STATUS.FETCHED
          };
          return (0, _immutabilityHelper.default)(state, {
            callHistory: {
              calls: {
                $set: calls
              }
            }
          });
        }

      case 'REQUESTING_FIELD_TRIPS':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              requests: {
                status: {
                  $set: _constants.FETCH_STATUS.FETCHING
                }
              }
            }
          });
        }

      case 'RECEIVED_FIELD_TRIPS':
        {
          const data = action.payload.fieldTrips;
          const requests = {
            data: data.sort((a, b) => (0, _moment.default)(b.endTime) - (0, _moment.default)(a.endTime)),
            status: _constants.FETCH_STATUS.FETCHED
          };
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              requests: {
                $set: requests
              }
            }
          });
        }

      case 'SET_FIELD_TRIP_FILTER':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              filter: {
                $merge: action.payload
              }
            }
          });
        }

      case 'SET_GROUP_SIZE':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              groupSize: {
                $set: action.payload
              }
            }
          });
        }

      case 'SET_ACTIVE_FIELD_TRIP':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              activeId: {
                $set: action.payload
              },
              groupSize: {
                $set: null
              }
            }
          });
        }

      case 'RECEIVED_FIELD_TRIP_DETAILS':
        {
          const {
            fieldTrip
          } = action.payload;
          const index = state.fieldTrip.requests.data.findIndex(req => req.id === fieldTrip.id);
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              requests: {
                data: {
                  [index]: {
                    $set: fieldTrip
                  }
                }
              }
            }
          });
        }

      case 'RECEIVED_TRAVEL_DATE_TRIPS':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              travelDateTripsInUse: {
                $set: action.payload
              }
            }
          });
        }

      case 'RECEIVE_TRIP_HASH':
        {
          const {
            hash,
            tripId
          } = action.payload;
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              tripHashLookup: {
                [tripId]: {
                  $set: hash
                }
              }
            }
          });
        }

      case 'CLEAR_SAVEABLE':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              saveable: {
                $set: {}
              }
            }
          });
        }

      case 'SET_SAVEABLE':
        {
          // The payload represents whether the saveable set of itineraries are for
          // the inbound or outbound journey.
          const saveableUpdate = {
            [action.payload ? 'outbound' : 'inbound']: true
          };
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              saveable: {
                $set: saveableUpdate
              }
            }
          });
        }

      case 'RECEIVED_QUERIES':
        {
          const {
            callId,
            queries
          } = action.payload;
          const {
            data
          } = state.callHistory.calls;
          const index = data.findIndex(call => call.id === callId);
          const call = { ...data[index],
            queries
          };
          return (0, _immutabilityHelper.default)(state, {
            callHistory: {
              calls: {
                data: {
                  [index]: {
                    $set: call
                  }
                }
              }
            }
          });
        }

      case 'ROUTING_RESPONSE':
        {
          const {
            searchId
          } = action.payload;

          if (state.activeCall) {
            // If call is in progress, record search ID when a routing response is
            // fulfilled, except in the case where the
            // searchId contains _CALL and call history window is visible, which indicates that a user is viewing a
            // past call record
            // TODO: How should we handle routing errors.
            if (!(state.callHistory.visible && searchId.indexOf('_CALL') !== -1)) {
              return (0, _immutabilityHelper.default)(state, {
                activeCall: {
                  searches: {
                    $push: [searchId]
                  }
                }
              });
            }
          } // Otherwise, ignore.


          return state;
        }

      case 'STORE_SESSION':
        {
          const {
            session
          } = action.payload;

          if (!session || !session.username) {
            const sessionId = session ? session.sessionId : 'N/A'; // Session is invalid if username is missing.

            window.alert(`Session ID ${sessionId} is invalid!`); // TODO: Should we return to URL_ROOT at this point?

            return (0, _immutabilityHelper.default)(state, {
              session: {
                $set: null
              }
            });
          }

          return (0, _immutabilityHelper.default)(state, {
            session: {
              $set: session
            }
          });
        }

      case 'TOGGLE_CALL_HISTORY':
        {
          return (0, _immutabilityHelper.default)(state, {
            callHistory: {
              visible: {
                $set: !state.callHistory.visible
              }
            }
          });
        }

      case 'TOGGLE_FIELD_TRIPS':
        {
          return (0, _immutabilityHelper.default)(state, {
            fieldTrip: {
              activeId: {
                $set: null
              },
              visible: {
                $set: !state.fieldTrip.visible
              }
            }
          });
        }

      case 'TOGGLE_MAILABLES':
        {
          return (0, _immutabilityHelper.default)(state, {
            mailables: {
              visible: {
                $set: !state.mailables.visible
              }
            }
          });
        }

      case 'END_CALL':
        {
          return (0, _immutabilityHelper.default)(state, {
            activeCall: {
              $set: null
            }
          });
        }

      default:
        return state;
    }
  };
}

var _default = createCallTakerReducer;
exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=call-taker.js