import { createHandlerChainRunner, Handler, HasOnComplete } from "@insite/client-framework/HandlerCreator";
import { getWishList, GetWishListApiParameter } from "@insite/client-framework/Services/WishListService";
import { WishListModel } from "@insite/client-framework/Types/ApiModels";

export interface LoadWishListResult {
    apiResult: WishListModel;
}

type HandlerType = Handler<GetWishListApiParameter & HasOnComplete<LoadWishListResult>, LoadWishListResult>;

export const DispatchBeginLoadWishList: HandlerType = props => {
    props.dispatch({
        type: "Data/WishLists/BeginLoadWishList",
        wishListId: props.parameter.wishListId,
    });
};

export const RequestDataFromApi: HandlerType = async props => {
    try {
        const { onComplete, ...apiParameter } = props.parameter;
        props.apiResult = await getWishList(apiParameter);
    } catch (error) {
        if ("status" in error && error.status === 404) {
            props.dispatch({
                type: "Data/WishLists/FailedToLoadWishList",
                wishListId: props.parameter.wishListId,
                status: 404,
            });
            return false;
        }
        throw error;
    }
};

export const DispatchCompleteLoadWishList: HandlerType = ({ dispatch, apiResult }) => {
    dispatch({
        type: "Data/WishLists/CompleteLoadWishList",
        wishList: apiResult,
    });
};

export const DispatchSetWishListTags: HandlerType = ({ dispatch, apiResult }) => {
    dispatch({
        type: "Pages/MyListDetails/SetWishListTags",
        wishListTags: apiResult.wishListTags?.map(o => o.tag) || [],
    });
};

export const chain = [
    DispatchBeginLoadWishList,
    RequestDataFromApi,
    DispatchCompleteLoadWishList,
    DispatchSetWishListTags,
];

const loadWishList = createHandlerChainRunner(chain, "LoadWishList");
export default loadWishList;
