import { types } from "../actions/actionTypes";

const initialState = {
  appStatus: {
    showLoadingPanel: { show: true, type: "launching" },
    isLoading: true,
    webOSVersion:"",  //"5.0", "9.0" ...
    serverHOST: "", //"US.nextlgsdp.com",
    cursorVisible: false
  },
  httpHeader: null
};

export const commonReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.CHANGE_APP_STATUS: {
      let isUpdated = false;

      for (let i in action.payload) {
        if (typeof action.payload[i] === "object") {
          if (
            JSON.stringify(action.payload[i]) !==
            JSON.stringify(state.appStatus[i])
          ) {
            isUpdated = true;

            break;
          }
        } else if (state.appStatus[i] !== action.payload[i]) {
          isUpdated = true;

          break;
        }
      }

      if (isUpdated) {
        return {
          ...state,
          appStatus: { ...state.appStatus, ...action.payload },
        };
      }
      return state;
    }
    case types.GET_HTTP_HEADER:
      return {
        ...state,
        httpHeader: action.payload
      }
    default:
      return state;
  }
};

export default commonReducer;
