import { PaymentBalanceTransaction } from "../../api";
import { ComponentErrorSeverity } from "../../api/ComponentError";
import { getErrorCode, getErrorMessage } from "../../api/services/utils";
export const makeGetPaymentTransactions = ({ id, authToken, service }) => async ({ params, onSuccess, onError, final }) => {
    var _a;
    try {
        const response = await service.fetchPaymentTransactions(id, authToken, params);
        if (!response.error) {
            const balanceTransactions = response.data.map((dataItem) => new PaymentBalanceTransaction(dataItem));
            const pagingInfo = Object.assign({}, response.page_info);
            onSuccess({ balanceTransactions, pagingInfo });
        }
        else {
            const responseError = getErrorMessage(response.error);
            const code = getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            onError({
                error: responseError,
                code,
                severity: ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        onError({
            error: getErrorMessage(error),
            code,
            severity: ComponentErrorSeverity.ERROR,
        });
    }
    finally {
        final();
    }
};
