/*
 *
 * WebPush reducer
 *
 */

import { fromJS } from "immutable";
import * as types from "./constants";

const initialState = fromJS({
  createTemplateInProgress: false,
  response: {},
  createTemplateErrorMessage: "",
  editTemplateInProgress: false,
  editResponse: {},
  editTemplateErrorMessage: "",
  uploadedAssetData: {},
  uploadAssetSuccess: false,
  assetUploading: false,
});

export function webPushReducer(state = initialState, action) {
  // Handle null or undefined action
  if (!action || typeof action !== 'object') {
    return state;
  }

  switch (action.type) {
    case types.DEFAULT_ACTION:
      return state;
    case types.CREATE_TEMPLATE_REQUEST:
      return state
        .set("createTemplateInProgress", true)
        .set("createTemplateError", false)
        .set("createTemplateErrorMessage", fromJS(""));
    case types.CREATE_TEMPLATE_SUCCESS:
      return state
        .set("createTemplateInProgress", false)
        .set("response", action.data)
        .set(
          "createTemplateError",
          action.statusCode !== undefined && action.statusCode > 300
        );
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set("createTemplateInProgress", false)
        .set("createTemplateError", true)
        .set("createTemplateErrorMessage", fromJS(action.error));
    case types.CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set("response", {});
    case types.EDIT_TEMPLATE_REQUEST:
      return state
        .set("editTemplateInProgress", true)
        .set("editTemplateError", false)
        .set("editTemplateErrorMessage", fromJS(""));
    case types.EDIT_TEMPLATE_SUCCESS:
      return state
        .set("editTemplateInProgress", false)
        .set("editResponse", action.data)
        .set(
          "editTemplateError",
          action.statusCode !== undefined && action.statusCode > 300
        );
    case types.EDIT_TEMPLATE_FAILURE:
      return state
        .set("editTemplateInProgress", false)
        .set("editTemplateError", true)
        .set("editTemplateErrorMessage", fromJS(action.error || action.errorMsg || ""));
    case types.CLEAR_EDIT_RESPONSE_REQUEST:
      return state.set("editResponse", {});
    case types.UPLOAD_WEBPUSH_ASSET_REQUEST:
      return state.set("uploadAssetSuccess", false).set("assetUploading", true);
    case types.UPLOAD_WEBPUSH_ASSET_SUCCESS:
      return state
        .set(
          "uploadAssetSuccess",
          action.statusCode !== undefined &&
            action.statusCode !== "" &&
            action.statusCode < 300,
        )
        .set("assetUploading", false)
        .set(
          action.templateType !== undefined
            ? `uploadedAssetData${action.templateType}`
            : "uploadedAssetData",
          action.data,
        );
    case types.UPLOAD_WEBPUSH_ASSET_FAILURE:
      return state
        .set("uploadAssetSuccess", false)
        .set("assetUploading", false);
    case types.CLEAR_WEBPUSH_ASSET:
      return state.delete(
        action.templateType !== undefined
          ? `uploadedAssetData${action.templateType}`
          : "uploadedAssetData",
      );
    default:
      return state;
  }
}

export default webPushReducer;

