import { useCallback, useEffect } from 'react';
import { useToast } from './ToastProvider';
var DescriptionBuilderStatus;
(function (DescriptionBuilderStatus) {
    DescriptionBuilderStatus["Success"] = "success";
    DescriptionBuilderStatus["Error"] = "error";
})(DescriptionBuilderStatus || (DescriptionBuilderStatus = {}));
export const useMutationsHandler = ({ mainMutation, dependantMutations, messageDescriptionBuilder, toastProps, ...rest }) => {
    const { showToast } = useToast();
    const mutations = [
        mainMutation,
        ...(dependantMutations ? dependantMutations : []),
    ];
    const handleMutationsCompletion = useCallback(async () => {
        var _a, _b, _c, _d, _e;
        const results = await Promise.all(mutations.map((m) => m.mutation));
        const loadingMutations = mutations.filter((_, index) => results[index].isLoading);
        const successMutations = mutations.filter((_, index) => results[index].isSuccess);
        const errorMutations = mutations.filter((_, index) => results[index].isError);
        const mainMutationDesc = {
            data: (_a = mainMutation.mutation) === null || _a === void 0 ? void 0 : _a.data,
            status: DescriptionBuilderStatus.Success,
            name: mainMutation.name,
        };
        const descriptionBuilders = [
            mainMutationDesc,
            ...((dependantMutations === null || dependantMutations === void 0 ? void 0 : dependantMutations.map(({ mutation, name }) => ({
                data: mutation.data,
                error: mutation.isError && mutation.error,
                status: mutation.isSuccess
                    ? DescriptionBuilderStatus.Success
                    : DescriptionBuilderStatus.Error,
                name,
            }))) || []),
        ];
        if (loadingMutations.length === 0) {
            if (errorMutations.length > 0) {
                if ((_b = mainMutation.mutation) === null || _b === void 0 ? void 0 : _b.isSuccess) {
                    'onMainMutationSuccess' in rest && ((_c = rest === null || rest === void 0 ? void 0 : rest.onMainMutationSuccess) === null || _c === void 0 ? void 0 : _c.call(rest));
                }
                showToast({
                    open: true,
                    status: 'error',
                    message: messageDescriptionBuilder(descriptionBuilders),
                    ...toastProps,
                });
                return;
            }
            else if (successMutations.length > 0) {
                'onMainMutationSuccess' in rest && ((_d = rest === null || rest === void 0 ? void 0 : rest.onMainMutationSuccess) === null || _d === void 0 ? void 0 : _d.call(rest));
                if (successMutations.length === mutations.length) {
                    'onAllMutationsSuccess' in rest && ((_e = rest === null || rest === void 0 ? void 0 : rest.onAllMutationsSuccess) === null || _e === void 0 ? void 0 : _e.call(rest));
                }
                showToast({
                    open: true,
                    status: 'success',
                    message: messageDescriptionBuilder(descriptionBuilders),
                    ...(toastProps ? toastProps : {}),
                });
            }
        }
    }, [JSON.stringify(mutations)]);
    useEffect(() => {
        handleMutationsCompletion();
    }, [handleMutationsCompletion]);
};
