'use strict';

var Plaid = require('./Plaid-CPESyiiV.js');
var utils = require('./utils-BsnepLy6.js');

const makeGetCheckout = ({ authToken, checkoutId, service }) => async ({ onSuccess, onError }) => {
    var _a;
    try {
        const response = await service.fetchCheckout(authToken, checkoutId);
        if (!response.error) {
            const checkout = response.data;
            onSuccess({ checkout });
        }
        else {
            const responseError = utils.getErrorMessage(response.error);
            const code = utils.getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            return onError({
                error: responseError,
                code,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = utils.getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: Plaid.ComponentErrorSeverity.ERROR,
        });
    }
};
const makeCheckoutComplete = ({ authToken, checkoutId, service }) => async ({ payment, onSuccess, onError }) => {
    var _a;
    try {
        const response = await service.complete(authToken, checkoutId, payment);
        if (!response.error) {
            const checkout = response.data;
            onSuccess({ checkout });
        }
        else {
            const responseError = utils.getErrorMessage(response.error);
            const code = utils.getErrorCode((_a = response.error) === null || _a === void 0 ? void 0 : _a.code);
            return onError({
                error: responseError,
                code,
                severity: Plaid.ComponentErrorSeverity.ERROR,
            });
        }
    }
    catch (error) {
        const code = utils.getErrorCode(error === null || error === void 0 ? void 0 : error.code);
        return onError({
            error: error.message || error,
            code,
            severity: Plaid.ComponentErrorSeverity.ERROR,
        });
    }
};

exports.makeCheckoutComplete = makeCheckoutComplete;
exports.makeGetCheckout = makeGetCheckout;
