import { createSlice } from '@reduxjs/toolkit';
import { PRIMARY_SCORE_TYPE } from '@root/constants';

export const userInformationSlice = createSlice({
    name: 'userInformation',
    initialState: {
        userInfo:{}
    },
    reducers: {
        fetchUserInfo: (state, action) => {
            // set default scale type to 1-100
            const { selectedRgPrimayScoreEnum = PRIMARY_SCORE_TYPE.ONE_TO_HUNDRED, ...userInfo } = action.payload;
            state.userInfo = { ...userInfo, selectedRgPrimayScoreEnum };
        }
    }
});

export const isRgPlusUser = state => {
    const modelVersionRG = (state?.userInfo?.userInfo?.rgModelVersion === 3
     || state?.userInfo?.userInfo?.rGModelVersion === 3);
    const hasRiskGaugePlusCap = state?.userInfo?.userInfo?.hasRiskGaugePlusCap;
    return modelVersionRG && hasRiskGaugePlusCap;
};

export const isEsgEnabled = state => {
    const rgPlus = isRgPlusUser(state);
    const hasEsgCap = state?.userInfo?.userInfo?.hasEsgCap;
    return rgPlus && hasEsgCap;
};

export const decimalSettings = state => {
    const numberOfDecimalPlaces = state?.userInfo?.userInfo?.numberofDecimalPlaces >= 0 ?
        state?.userInfo?.userInfo?.numberofDecimalPlaces : 2;
    return numberOfDecimalPlaces > 6 ? 6 : numberOfDecimalPlaces;
};

export const isMaxLimitEnabled = state => {
    const rgPlus = isRgPlusUser(state);
    const hasMaxLimitCap = state?.userInfo?.userInfo?.maxLimitVersion === 2;
    return rgPlus && hasMaxLimitCap;
};

export const { fetchUserInfo } = userInformationSlice.actions;
export default userInformationSlice.reducer;
