import { createHandlerChainRunner, Handler } from "@insite/client-framework/HandlerCreator";
import { enablePeeriusRequestsConsoleLogs } from "@insite/client-framework/Store/Components/ProductRecommendations/ProductRecommendationsSelector";

type HandlerType = Handler<{ message: string; type?: "info" | "error"; data?: any }>;

export const CheckIfLoggingEnabled: HandlerType = ({ getState }) => {
    if (!enablePeeriusRequestsConsoleLogs(getState())) {
        return false;
    }
};

export const Log: HandlerType = ({ parameter: { message, type, data } }) => {
    if (type === "error") {
        // eslint-disable-next-line no-console
        console.error(`Opti - ${message}`, data);
    } else {
        // eslint-disable-next-line no-console
        console.log(`Opti - ${message}`, data);
    }
};

export const chain = [CheckIfLoggingEnabled, Log];

const log = createHandlerChainRunner(chain, "Log");
export default log;
