import {
    ApiHandlerDiscreteParameter,
    createHandlerChainRunner,
    markSkipOnCompleteIfOnSuccessIsSet,
} from "@insite/client-framework/HandlerCreator";
import { API_URL_CURRENT_FRAGMENT } from "@insite/client-framework/Services/ApiService";
import {
    UpdateCartLineApiParameter,
    updateCartLineWithResult as updateCartLineApi,
} from "@insite/client-framework/Services/CartService";
import loadCurrentCart from "@insite/client-framework/Store/Data/Carts/Handlers/LoadCurrentCart";
import { CartLineModel } from "@insite/client-framework/Types/ApiModels";

interface UpdateCartLineParameter {
    onSuccess?: () => void;
    cartLine: CartLineModel;
    shouldNotReloadPromotions?: boolean;
    shouldNotLoadFullCart?: boolean;
}

type HandlerType = ApiHandlerDiscreteParameter<UpdateCartLineParameter, UpdateCartLineApiParameter>;

export const DispatchBeginUpdateCartLine: HandlerType = props => {
    props.dispatch({
        type: "Context/BeginUpdatingCart",
    });
    props.dispatch({
        type: "Pages/Cart/BeginUpdateCartLine",
    });
};

export const PopulateApiParameter: HandlerType = props => {
    props.apiParameter = {
        ...props.parameter,
        cartId: API_URL_CURRENT_FRAGMENT,
    };
};

export const UpdateCartLine: HandlerType = async props => {
    await updateCartLineApi(props.apiParameter);
};

export const LoadCart: HandlerType = props => {
    props.parameter.shouldNotLoadFullCart
        ? props.dispatch(
              loadCurrentCart({
                  shouldLoadFullCart: false,
                  getCartLines: true,
                  getCostCodes: true,
                  getPromotions: !props.parameter.shouldNotReloadPromotions,
              }),
          )
        : props.dispatch(loadCurrentCart({ getPromotions: !props.parameter.shouldNotReloadPromotions }));
};

/** @deprecated Not needed anymore */
export const LoadPromotions: HandlerType = () => {};

export const DispatchCompleteUpdateCartLine: HandlerType = props => {
    props.dispatch({
        type: "Pages/Cart/CompleteUpdateCartLine",
    });
    props.dispatch({
        type: "Context/CompleteUpdatingCart",
    });
};

export const ExecuteOnSuccessCallback: HandlerType = props => {
    markSkipOnCompleteIfOnSuccessIsSet(props);
    props.parameter.onSuccess?.();
};

export const chain = [
    DispatchBeginUpdateCartLine,
    PopulateApiParameter,
    UpdateCartLine,
    LoadCart,
    LoadPromotions,
    DispatchCompleteUpdateCartLine,
    ExecuteOnSuccessCallback,
];

const updateCartLine = createHandlerChainRunner(chain, "UpdateCartLine");
export default updateCartLine;
