import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  // TODO: 각종 Status 추가
  appStatus: {
    showLoadingPanel: { show: true, type: "launching" },
    isLoading: true,
    cursorVisible: false,
  },
};

export const commonSlice = createSlice({
  name: "common",
  initialState,
  reducers: {
    changeAppStatus: (state, action) => {
      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) {
        state.appStatus = { ...state.appStatus, ...action.payload };
      }
    },
  },
});

export const { changeAppStatus } = commonSlice.actions;

export default commonSlice.reducer;
