import {
    ApiHandlerDiscreteParameter,
    createHandlerChainRunnerOptionalParameter,
} from "@insite/client-framework/HandlerCreator";
import logger from "@insite/client-framework/Logger";
import { getSettings, GetSettingsApiParameter, SettingsModel } from "@insite/client-framework/Services/SettingsService";

type HandlerType = ApiHandlerDiscreteParameter<{}, GetSettingsApiParameter, SettingsModel>;

export const DispatchBeginLoadSettings: HandlerType = props => {
    props.dispatch({
        type: "Context/BeginLoadSettings",
    });
};

export const PopulateApiParameter: HandlerType = props => {
    props.apiParameter = {};
};

export const RequestDataFromApi: HandlerType = async props => {
    const settings = await getSettings(props.apiParameter);
    if (!settings) {
        logger.warn(`Unable to load website settings in LoadSettings.`);
        return false;
    }
    props.apiResult = settings;
};

export const DispatchCompleteLoadSettings: HandlerType = props => {
    props.dispatch({
        type: "Context/CompleteLoadSettings",
        settings: props.apiResult,
    });
};

export const chain = [DispatchBeginLoadSettings, RequestDataFromApi, DispatchCompleteLoadSettings];

const loadSettings = createHandlerChainRunnerOptionalParameter(chain, {}, "LoadSettings");
export default loadSettings;
