import { useCallback, useEffect } from 'react';
import { useToast } from './ToastProvider';
var DescriptionBuilderStatus;
(function (DescriptionBuilderStatus) {
    DescriptionBuilderStatus["Success"] = "success";
    DescriptionBuilderStatus["Error"] = "error";
    DescriptionBuilderStatus["Idle"] = "idle";
})(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, _f, _g;
        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: ((_b = mainMutation.mutation) === null || _b === void 0 ? void 0 : _b.isSuccess)
                ? DescriptionBuilderStatus.Success
                : ((_c = mainMutation.mutation) === null || _c === void 0 ? void 0 : _c.isIdle)
                    ? DescriptionBuilderStatus.Idle
                    : DescriptionBuilderStatus.Error,
            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 : null,
                status: mutation.isSuccess
                    ? DescriptionBuilderStatus.Success
                    : mutation.isIdle
                        ? DescriptionBuilderStatus.Idle
                        : DescriptionBuilderStatus.Error,
                name,
            }))) || []),
        ];
        if (loadingMutations.length === 0) {
            if (errorMutations.length > 0) {
                if ((_d = mainMutation.mutation) === null || _d === void 0 ? void 0 : _d.isSuccess) {
                    'onMainMutationSuccess' in rest && ((_e = rest === null || rest === void 0 ? void 0 : rest.onMainMutationSuccess) === null || _e === void 0 ? void 0 : _e.call(rest));
                }
                showToast({
                    open: true,
                    status: 'error',
                    message: messageDescriptionBuilder(descriptionBuilders),
                    ...toastProps,
                });
                return;
            }
            else if (successMutations.length > 0) {
                'onMainMutationSuccess' in rest && ((_f = rest === null || rest === void 0 ? void 0 : rest.onMainMutationSuccess) === null || _f === void 0 ? void 0 : _f.call(rest));
                if (successMutations.length === mutations.length) {
                    'onAllMutationsSuccess' in rest && ((_g = rest === null || rest === void 0 ? void 0 : rest.onAllMutationsSuccess) === null || _g === void 0 ? void 0 : _g.call(rest));
                }
                showToast({
                    open: true,
                    status: 'success',
                    message: messageDescriptionBuilder(descriptionBuilders),
                    ...(toastProps ? toastProps : {}),
                });
            }
        }
    }, [JSON.stringify(mutations)]);
    useEffect(() => {
        handleMutationsCompletion();
    }, [handleMutationsCompletion]);
};
