import waitFor from "@insite/client-framework/Common/Utilities/waitFor";
import { createHandlerChainRunner, Handler, HasOnSuccess } from "@insite/client-framework/HandlerCreator";
import deleteWishListLine from "@insite/client-framework/Store/Pages/MyListDetails/Handlers/DeleteWishListLine";
import updateQuantity from "@insite/client-framework/Store/Pages/MyListDetails/Handlers/UpdateQuantity";
import updateWishListLine from "@insite/client-framework/Store/Pages/MyListDetails/Handlers/UpdateWishListLine";
import updateWishListLineQuantities from "@insite/client-framework/Store/Pages/MyListDetails/Handlers/UpdateWishListLineQuantities";
import { WishListLineModel } from "@insite/client-framework/Types/ApiModels";

interface Parameter extends HasOnSuccess {
    wishListLine: WishListLineModel;
    wishListLines: WishListLineModel[];
    wishListId: string;
}

interface Props {
    existingWishListLine: WishListLineModel | undefined;
    quantityUpdated: boolean;
}

type HandlerType = Handler<Parameter, Props>;

export const CheckIfProductAlreadyExistsInWishList: HandlerType = props => {
    const wishListLines = props.parameter.wishListLines;
    const replacementProductId = props.parameter.wishListLine?.replacementProduct?.id;

    if (!replacementProductId) {
        return false;
    }

    props.existingWishListLine = wishListLines.find(line => line.productId === replacementProductId);
};

export const UpdateQuantity: HandlerType = async props => {
    const { wishListLine } = props.parameter;
    const { existingWishListLine } = props;

    if (!existingWishListLine) {
        return;
    }

    const updatedQuantity = existingWishListLine.qtyOrdered + wishListLine.qtyOrdered;
    props.quantityUpdated = false;

    props.dispatch(
        updateQuantity({
            wishListLineId: existingWishListLine.id,
            qtyOrdered: updatedQuantity,
            onComplete: () => {
                props.dispatch(
                    updateWishListLineQuantities({
                        onSuccess: () => {
                            props.quantityUpdated = true;
                        },
                    }),
                );
            },
        }),
    );

    await waitFor(() => !!props.quantityUpdated);
};

export const DeleteLine: HandlerType = props => {
    const { existingWishListLine } = props;
    const { wishListId, wishListLine } = props.parameter;

    if (!existingWishListLine) {
        return;
    }

    props.dispatch(
        deleteWishListLine({
            wishListId,
            wishListLineId: wishListLine.id,
        }),
    );
};

export const UpdateLine: HandlerType = props => {
    const { wishListId, wishListLine } = props.parameter;
    const replacementProductId = wishListLine.replacementProduct?.id;

    if (!replacementProductId) {
        return;
    }

    if (props.existingWishListLine) {
        return;
    }

    props.dispatch(
        updateWishListLine({
            wishListId,
            wishListLineId: wishListLine.id,
            wishListLine: {
                ...wishListLine,
                productId: replacementProductId,
            },
        }),
    );
};

export const chain = [CheckIfProductAlreadyExistsInWishList, UpdateQuantity, DeleteLine, UpdateLine];

const replaceProduct = createHandlerChainRunner(chain, "ReplaceProduct");
export default replaceProduct;
