var checkoutjs_module = 'paystand';
var core_domain = 'paystand.com';
var api_domain = 'api.paystand.com';
var checkout_domain = 'checkout.paystand.com';
var use_sandbox = window.checkoutConfig.payment.paystandmagento.use_sandbox;
if (use_sandbox == '1') {
    checkoutjs_module = 'paystand-sandbox';
    core_domain = 'paystand.co';
    api_domain = 'api.paystand.co';
    checkout_domain = 'checkout.paystand.co';
    // core_domain = 'localhost:3001';
    // api_domain = 'localhost:3001/api';
    // checkout_domain = 'localhost:3003';
}

define(
    [
        'jquery',
        'Magento_Checkout/js/view/payment/default',
        'Magento_Checkout/js/model/quote',
        'Magento_CheckoutAgreements/js/model/agreement-validator',
        checkoutjs_module,
    ],
    function ($, Component, quote, agreementValidator, paystand) {
        'use strict';


        const loadPaystandCheckout = function () {

            // Get information from Magento checkout to load Paystand Checkout with
            const publishable_key = window.checkoutConfig.payment.paystandmagento.publishable_key;
            const price = quote.totals().grand_total.toString();
            const quoteId = quote.getQuoteId();
            const billing = quote.billingAddress();

            let checkoutData = {
                publishable_key: publishable_key,
                price: price,
                quoteId: quoteId,
                billing: billing,
            }

            // Init checkout with iso3 country code if country is provided
            if (billing.countryId) {
                $.ajax({
                    beforeSend: function (request) {
                        request.setRequestHeader("x-publishable-key", publishable_key);
                    },
                    dataType: "text",
                    contentType: "application/json; charset=utf-8",
                    url: "https://" + api_domain + "/v3/addresses/countries/iso?code=" + billing.countryId,
                    success: function (data) {
                        checkoutData.countryISO3 = JSON.parse(data).iso3;
                        initCheckout(checkoutData);
                    },
                    error: function (error) {
                        console.log('Unable to get ISO3 code from PayStand!');
                    },
                });
            } else {
                initCheckout(checkoutData);
            }
        };

        const initCheckout= function (checkoutData){
            let config = {
                "publishableKey": checkoutData.publishable_key,
                "paymentAmount": checkoutData.price,
                "fixedAmount": true,
                "viewReceipt": "close",
                "viewCheckout": "mobile",
                "paymentCurrency": "USD",
                "payerName": checkoutData.billing.firstname + ' ' + checkoutData.billing.lastname,
                "payerEmail": quote.guestEmail,
                "payerAddressCounty": checkoutData.countryISO3,
                "paymentMeta": {
                    "source": "magento 2",
                    "quote": checkoutData.quoteId,
                    "quoteDetails" : quote.totals()
                }
            };

            if (checkoutData.billing.street && checkoutData.billing.street.length > 0) {
                config.payerAddressStreet = checkoutData.billing.street[0];
            }
            if (checkoutData.billing.city) {
                config.payerAddressCity = checkoutData.billing.city;
            }
            if (checkoutData.billing.postcode) {
                config.payerAddressPostal = checkoutData.billing.postcode;
            }
            if (checkoutData.billing.regionCode) {
                config.payerAddressState = checkoutData.billing.regionCode;
            }

            console.log("rebooting checkout with config", config);

            //This block fixes the issue where checkout opens blank
            psCheckout.onReady(function () {
                // wait for reboot to complete before showing checkout
                psCheckout.onceLoaded(function (data) {
                    psCheckout.showCheckout();
                });
                // reboot checkout with a new config
                psCheckout.reboot(config);
            });

            psCheckout.reboot(config);
            psCheckout.showCheckout();
        }

        // Validate agreement section using core Magento 2 validator
        let validateAgreementSection = function() {
            if(agreementValidator.validate()){
                // if we clear agreement section, click actual ps-button to open checkout
                $(".ps-button").click();
            }
        }

        psCheckout.onComplete(function(data){
            console.log("custom checkout complete:", data);
            $(".submit-trigger").click();
        });

        psCheckout.onError(function(data){
            console.log("custom checkout error:", data);
        });

        psCheckout.checkoutEvent = function (data) {
            console.log(data.response.event.type + ":", data);
        }


        return Component.extend({
            defaults: {
                template: 'PayStand_PayStandMagento/payment/paystandmagento-directpost'
            },

            // this function is binded to Magento's "Pay with Paystand" button
            validateOpenCheckout: function () {
                validateAgreementSection();
            },

            // this function ins binded to actual Paystand button to trigger checkout
            loadPaystandCheckout: function (event) {
                loadPaystandCheckout();
            }
        });
    }
);

@@ -2448,16 +2448,17 @@
 Checkout
+ 
 = functi
@@ -2470,24 +2470,25 @@
 heckoutData)
+ 
 %7B%0A          
@@ -3154,17 +3154,16 @@
 Details%22
- 
 : quote.
@@ -4485,16 +4485,17 @@
 function
+ 
 () %7B%0A   
@@ -4505,16 +4505,17 @@
       if
+ 
 (agreeme
@@ -4537,16 +4537,17 @@
 idate())
+ 
 %7B%0A      
@@ -4726,38 +4726,40 @@
 omplete(function
+ 
 (data)
+ 
 %7B%0A            co
@@ -4894,22 +4894,24 @@
 function
+ 
 (data)
+ 
 %7B%0A      
@@ -4973,32 +4973,72 @@
     %7D);%0A%0A       
+ // uncomment for development%0A        //
  psCheckout.chec
@@ -5066,32 +5066,35 @@
 (data) %7B%0A       
+ //
      console.log
@@ -5136,24 +5136,27 @@
 ta);%0A       
+ //
  %7D%0A%0A%0A       

remove console logs and fix format