import { createHandlerChainRunner, Handler } from "@insite/client-framework/HandlerCreator";
import { sendAddToWishListEvent } from "@insite/client-framework/OdpEvents/UpdateWishListEvent";
import {
    addWishListLines as addWishListLinesApi,
    AddWishListLinesApiParameter,
} from "@insite/client-framework/Services/WishListService";
import loadWishLists from "@insite/client-framework/Store/Pages/MyLists/Handlers/LoadWishLists";

type Parameter = {
    apiParameter: AddWishListLinesApiParameter;
    reloadWishLists?: boolean;
    onSuccess?: () => void;
};
type Props = {
    apiResult: {
        error: boolean;
        message?: string;
    };
};
type HandlerType = Handler<Parameter, Props>;

export const DispatchBeginAddWishListLines: HandlerType = props => {
    props.dispatch({
        type: "Pages/MyLists/BeginAddWishListLines",
    });
};

export const CallAddWishListLinesApi: HandlerType = async props => {
    await addWishListLinesApi(props.parameter.apiParameter);
    props.apiResult = { error: false };
};

export const ResetWishListData: HandlerType = props => {
    props.dispatch({
        type: "Data/WishListLines/Reset",
    });
    props.dispatch({
        type: "Data/WishLists/Reset",
    });
};

export const SendAddToWishListEvent: HandlerType = props => {
    for (const line of props.parameter.apiParameter.lines) {
        sendAddToWishListEvent(
            {
                productId: line.productId,
                wishListId: props.parameter.apiParameter.wishList.id,
            },
            props.getState(),
        );
    }
};

export const ExecuteOnSuccessCallback: HandlerType = props => {
    if (!props.apiResult.error) {
        props.parameter.onSuccess?.();
    }
};

export const DispatchCompleteAddWishListLines: HandlerType = props => {
    props.dispatch({
        type: "Pages/MyLists/CompleteAddWishListLines",
    });
};

export const DispatchLoadWishLists: HandlerType = props => {
    if (props.parameter.reloadWishLists) {
        props.dispatch(loadWishLists());
    }
};

export const chain = [
    DispatchBeginAddWishListLines,
    CallAddWishListLinesApi,
    ResetWishListData,
    SendAddToWishListEvent,
    ExecuteOnSuccessCallback,
    DispatchCompleteAddWishListLines,
    DispatchLoadWishLists,
];

const addWishListLines = createHandlerChainRunner(chain, "AddWishListLines");
export default addWishListLines;
