"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.socket = socket;
var actions = _interopRequireWildcard(require("../constants/socketActionTypes"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const initialState = {
  id: null,
  channels: [],
  socketState: actions.CLOSED,
  authState: actions.PENDING,
  error: undefined
};
function socket() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  let action = arguments.length > 1 ? arguments[1] : undefined;
  switch (action.type) {
    case actions.CONNECT_REQUEST:
      {
        return {
          ...state,
          socketState: actions.CONNECTING
        };
      }
    case actions.CONNECT_ERROR:
      return {
        ...state,
        error: action.error
      };
    case actions.CONNECT_SUCCESS:
      return {
        ...state,
        id: action.payload.id,
        socketState: action.payload.socketState,
        authState: action.payload.authState,
        error: action.error
      };
    case actions.AUTH_REQUEST:
      return {
        ...state,
        authState: actions.PENDING
      };
    case actions.AUTH_SUCCESS:
      return {
        ...state,
        authState: actions.AUTHENTICATED,
        baseChannel: action.baseChannel
      };
    case actions.AUTH_ERROR:
      return {
        ...state,
        authState: actions.UNAUTHENTICATED,
        error: action.error
      };
    case actions.DEAUTHENTICATE:
      return {
        ...state,
        authState: actions.UNAUTHENTICATED,
        authToken: null
      };
    case actions.SUBSCRIBE_SUCCESS:
      return {
        ...state,
        channels: [...state.channels, action.channel]
      };
    case actions.UNSUBSCRIBE:
      return {
        ...state,
        channels: state.channels.filter(channel => channel !== action.channel)
      };
    case actions.DISCONNECTED:
      return initialState;
    default:
      return state;
  }
}