
import update from "immutability-helper";

import { EnumAccountActions } from "./AccountActions.js";


const initialState = {
    utente: null,
    userid: null,
    password: null,
    aliveStatus: {
        application: true,
        session: true
    },
    sessionExpired: false
}

function account(state, action) {
    if (typeof state === "undefined") {
        return initialState
    }

    switch (action.type) {
        case EnumAccountActions.LOGIN_SUCCESS:
            console.log("Welcome " + action.user.userId);
            return update(state, { utente: { $set: action.user } });

        case EnumAccountActions.SSO:
            return update(state, {
                userid: { $set: action.datiUtente.userId }
            });
    
        case EnumAccountActions.LOGIN_FAILED:
        case EnumAccountActions.LOGOUT:
            return update(state, { $set: initialState });

        case EnumAccountActions.UPDATE_FORM:
            return update(state, { [action.key]: { $set: action.value } });

        case EnumAccountActions.SESSION_EXPIRED:
            return update(state, { sessionExpired: { $set: true } });

        case EnumAccountActions.ALIVE_STATUS:
            return update(state, { aliveStatus: { $set: action.aliveStatus } });

        default:
            return state;
    }
}

export default account;