/* Minification failed. Returning unminified contents.
(5,5,9,6): run-time error JS1314: Implicit property name must be identifier: onInit(data, actions) {
        getPayPalActions = function () {
            return actions;
        }
    }
(10,5,12,6): run-time error JS1314: Implicit property name must be identifier: onApprove(data, actions) {
        document.getElementById('PayPalOrderID').value = data.orderID;
    }
(13,5,15,6): run-time error JS1314: Implicit property name must be identifier: onCancel() {
        getPayPalActions().disable();
    }
(16,5,20,6): run-time error JS1314: Implicit property name must be identifier: onError(err) {
        console.error("An error occured with PayPal", err);
        document.getElementById('PayPalError').value = (err);
        showPayPalFailedMessage();
    }
(24,5,28,6): run-time error JS1314: Implicit property name must be identifier: onInit(data, actions) {
        getVenmoActions = function () {
            return actions;
        }
    }
(29,5,31,6): run-time error JS1314: Implicit property name must be identifier: onApprove(data, actions) {
        document.getElementById('PayPalOrderID').value = (data.orderID);
    }
(32,5,34,6): run-time error JS1314: Implicit property name must be identifier: onCancel() {
        getVenmoActions().disable();
    }
(35,5,39,6): run-time error JS1314: Implicit property name must be identifier: onError(err) {
        console.error("An error occured with Venmo", err);
        //document.getElementById('PayPalError').value = (err);
        showVenmoFailedMessage();
    }
(53,5,56,6): run-time error JS1314: Implicit property name must be identifier: onInit(data, actions) {
        actionStatus = actions;
        actionStatus.disable();
    }
(57,5,68,6): run-time error JS1314: Implicit property name must be identifier: onClick() {
        updateRealAddress();
        updateFieldsOnSubmit();

        if (validateAndLogForm(document.getElementById('paypal-wrapper'))) {
            actionStatus.enable();
        } else {
            actionStatus.disable();

            scrollToFirstError();
        }
    }
(69,5,95,6): run-time error JS1314: Implicit property name must be identifier: createOrder(data, actions) {
        return actions.order.create({
            intent: "AUTHORIZE",
            purchase_units: [{
                amount: {
                    value: document.getElementById('Amount').value,
                    breakdown: {
                        item_total: {
                            currency_code: "USD",
                            value: document.getElementById('Amount').value
                        }
                    }
                },
                description: document.getElementById('AccountName').value.substring(0, 127),
                items: [{
                    name: ("Donation to " + document.getElementById('AccountName').value).substring(0, 127),
                    quantity: "1",
                    category: "DONATION",
                    unit_amount: {
                        currency_code: "USD",
                        value: document.getElementById('Amount').value
                    }
                }],
                custom_id: document.getElementById('DebugId').value,
            }]
        });
    }
(96,5,98,6): run-time error JS1314: Implicit property name must be identifier: onApprove(data, actions) {
        return submitAfterMarketplaceApproval();
    }
 */
console.log('PayPal Marketplace Code Connected')

let getPayPalActions, getVenmoActions;
const paypalEventHandlers = {
    onInit(data, actions) {
        getPayPalActions = function () {
            return actions;
        }
    },
    onApprove(data, actions) {
        document.getElementById('PayPalOrderID').value = data.orderID;
    },
    onCancel() {
        getPayPalActions().disable();
    },
    onError(err) {
        console.error("An error occured with PayPal", err);
        document.getElementById('PayPalError').value = (err);
        showPayPalFailedMessage();
    },
}

const venmoEventHandlers = {
    onInit(data, actions) {
        getVenmoActions = function () {
            return actions;
        }
    },
    onApprove(data, actions) {
        document.getElementById('PayPalOrderID').value = (data.orderID);
    },
    onCancel() {
        getVenmoActions().disable();
    },
    onError(err) {
        console.error("An error occured with Venmo", err);
        //document.getElementById('PayPalError').value = (err);
        showVenmoFailedMessage();
    },
}

document.addEventListener("DOMContentLoaded", function () {
    if (document.getElementById('btnPayPal')) {
        createPaymentMethodButton(paypal.FUNDING.PAYPAL, paypalEventHandlers);
    }

    if (document.getElementById('btnVenmo')) {
        createPaymentMethodButton(paypal.FUNDING.VENMO, venmoEventHandlers);
    }
})

const sharedEventHandlers = {
    onInit(data, actions) {
        actionStatus = actions;
        actionStatus.disable();
    },
    onClick() {
        updateRealAddress();
        updateFieldsOnSubmit();

        if (validateAndLogForm(document.getElementById('paypal-wrapper'))) {
            actionStatus.enable();
        } else {
            actionStatus.disable();

            scrollToFirstError();
        }
    },
    createOrder(data, actions) {
        return actions.order.create({
            intent: "AUTHORIZE",
            purchase_units: [{
                amount: {
                    value: document.getElementById('Amount').value,
                    breakdown: {
                        item_total: {
                            currency_code: "USD",
                            value: document.getElementById('Amount').value
                        }
                    }
                },
                description: document.getElementById('AccountName').value.substring(0, 127),
                items: [{
                    name: ("Donation to " + document.getElementById('AccountName').value).substring(0, 127),
                    quantity: "1",
                    category: "DONATION",
                    unit_amount: {
                        currency_code: "USD",
                        value: document.getElementById('Amount').value
                    }
                }],
                custom_id: document.getElementById('DebugId').value,
            }]
        });
    },
    onApprove(data, actions) {
        return submitAfterMarketplaceApproval();
    },
}

function createPaymentMethodButton(fundingSource, fundingSourceEventHandlers) {
    var config = {
        fundingSource: fundingSource,
        style: {
            layout: 'horizontal',
            shape: 'pill',
            tagline: false,
            funding: {
                disallowed: [paypal.FUNDING.CREDIT]
            }
        },
        onInit: function (data, actions) {
            fundingSourceEventHandlers.onInit(data, actions);
            sharedEventHandlers.onInit(data, actions);
        },
        onClick: function () {
            sharedEventHandlers.onClick();
        },
        createOrder: function (data, actions) {
            return sharedEventHandlers.createOrder(data, actions);
        },
        onApprove: function (data, actions) {
            fundingSourceEventHandlers.onApprove(data, actions);
            return sharedEventHandlers.onApprove(data, actions);
        },
        onCancel: function () {
            fundingSourceEventHandlers.onCancel();
        },
        onError: function (err) {
            fundingSourceEventHandlers.onError(err);
        }
    };
    var button = paypal.Buttons(config);
    if (button.isEligible()) {
        button.render(`#${fundingSource}-wrapper`);
    }
}

function submitAfterMarketplaceApproval() {
    document.querySelector('.efundForm').submit();
    disableAmountAffectors();
}

function disablePayPal() {
    // Hide Paypal tab
    if (document.getElementById('btnPayPal')) {
        document.getElementById('btnPayPal').style.display = 'none';
    }
    if (document.getElementById('paypal-tab-content')) {
        document.getElementById('paypal-tab-content').style.display = 'none';
    }

    //Click Credit Card if PayPal was selected
    if (document.getElementById('btnPayPal')) {
        let disabled = document.getElementById('btnPayPal').getAttribute('disabled');
        if (disabled !== undefined && disabled !== false) {
            handleCreditCardClick();
        }
    }
}

function enablePayPal() {
    // Show Paypal tab
    document.getElementById('btnPayPal').style.display = 'block';
}

function showPayPalFailedMessage() {
    document.getElementById('paypal-message').innerHTML = ('<p class="text-center">' + document.getElementById('trans-paypal3').textContent + '</p>')
    document.getElementById('paypal-message').style.display = 'block';
}

function showVenmoFailedMessage() {
    document.getElementById('venmo-message-error').innerHTML = ('<p class="text-center">' + document.getElementById('trans-venmo').textContent + '</p>')
    document.getElementById('venmo-message-error').style.display = 'block';
};
