import { ApiHandlerDiscreteParameter, createHandlerChainRunner } from "@insite/client-framework/HandlerCreator";
import {
    getSession,
    Session,
    updateSession,
    UpdateSessionApiParameter,
} from "@insite/client-framework/Services/SessionService";

type HandlerType = ApiHandlerDiscreteParameter<{ dashboardIsHomepage: boolean }, UpdateSessionApiParameter, Session>;

export const PopulateApiParameter: HandlerType = props => {
    props.apiParameter = {
        session: {
            dashboardIsHomepage: props.parameter.dashboardIsHomepage,
        } as Session,
    };
};

export const UpdateSession: HandlerType = async props => {
    await updateSession(props.apiParameter);
    props.apiResult = await getSession({});
};

export const DispatchCompleteLoadSession: HandlerType = props => {
    props.dispatch({
        type: "Context/CompleteLoadSession",
        session: props.apiResult,
    });
};

export const chain = [PopulateApiParameter, UpdateSession, DispatchCompleteLoadSession];

const setDashboardIsHomepage = createHandlerChainRunner(chain, "SetDashboardIsHomepage");
export default setDashboardIsHomepage;
