import { getUserMovingTextsService } from "../../../features/authentication/services/getUserMovingTexts";
import { getUserProfile } from "../../../features/authentication/services/getUserProfile";
import { getUserRoleInfo } from "../../../features/authentication/services/getUserRoleInfo";
import {
  changePassword,
  checkUserPath,
  loginSendMobile,
  loginWithPassword,
} from "../../../features/authentication/services/login";

export const userExtraReducers = {
  [loginSendMobile.fulfilled]: (state, { payload }) => {
    state.pending = false;
  },
  [loginSendMobile.pending]: (state) => {
    state.pending = true;
  },
  [loginWithPassword.fulfilled]: (state, { payload }) => {
    state.pending = false;
    state.authToken = payload.data.accessToken;
    state.tokenExpiresIn = payload.data.expiresIn;
    const filteredRoles = payload.data.role.filter(
      (role) => role !== "ProvinceInspector"
    );
    state.role = filteredRoles;
    state.loginUserProfile = payload.data;
  },
  [loginWithPassword.pending]: (state) => {
    state.pending = true;
  },
  [getUserProfile.fulfilled]: (state, { payload }) => {
    state.pending = false;
    state.userProfile = payload.data;
  },
  [checkUserPath.pending]: (state) => {
    state.pending = true;
  },
  [checkUserPath.fulfilled]: (state, { payload }) => {
    state.pending = false;
    state.userPath = payload.data.backend + "/";
  },
  [getUserProfile.pending]: (state) => {
    state.pending = true;
  },
  [changePassword.fulfilled]: (state) => {
    state.pending = false;
  },
  [changePassword.pending]: (state) => {
    state.pending = true;
  },
  [changePassword.pending]: (state) => {
    state.pending = true;
  },
  [getUserMovingTextsService.fulfilled]: (state, { payload }) => {
    let fulltext = "";
    if (payload.data.length) {
      payload.data.forEach((element) => {
        if (element !== "undefined") {
          fulltext =
            fulltext +
            "‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌ ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌ ‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌  ‌ ‌ ‌ ‌ " +
            element?.movingText;
        }
      });
    } else {
      fulltext = null;
    }
    state.pending = false;
    state.userMovingTexts = fulltext;
  },
  [getUserRoleInfo.fulfilled]: (state, { payload }) => {
    state.pending = false;
    if (payload.data && Array.isArray(payload.data)) {
      state.subUsers = payload.data;
      if (payload.data.length > 0) {
        state.selectedSubUser = payload.data[0];
      }
    } else {
      state.subUsers = [];
      state.selectedSubUser = null;
    }
  },
  [getUserRoleInfo.pending]: (state) => {
    state.pending = true;
  },
  [getUserRoleInfo.rejected]: (state) => {
    state.pending = false;
    state.subUsers = [];
    state.selectedSubUser = null;
  },
};
