(function (apiKey) {
    (function (p, e, n, d, o) {
        var v, w, x, y, z; o = p[d] = p[d] || {}; o._q = o._q || [];
        v = ['initialize', 'identify', 'updateOptions', 'pageLoad', 'track']; for (w = 0, x = v.length; w < x; ++w)(function (m) {
            o[m] = o[m] || function () { o._qm === v[0] ? 'unshift' : 'push'; };
        })(v[w]);
        y = e.createElement(n); y.async = !0; y.src = 'https://cdn.pendo.io/agent/static/' + apiKey + '/pendo.js';
        z = e.getElementsByTagName(n)[0]; z.parentNode.insertBefore(y, z);
    })(window, document, 'script', 'pendo');
    // This function creates visitors and accounts in Pendo
    // You will need to replace <visitor-id-goes-here> and <account-id-goes-here> with values you use in your app
    // Please use Strings, Numbers, or Bools for value types.

    GetCanTrackEvents(function (data) {
        if (data.d) {
            GetVisitorAndAccountIds(function (data1) {
                pendo.initialize({
                    visitor: {
                        id: data1.d.VisitorId
                        // email:        // Recommended if using Pendo Feedback, or NPS Email
                        // full_name:    // Recommended if using Pendo Feedback
                        // role:         // Optional

                        // You can add any additional visitor level key-values here,
                        // as long as it's not one of the above reserved names.
                    },

                    account: {
                        id: data1.d.AccountId
                        // name:         // Optional
                        // is_paying:    // Recommended if using Pendo Feedback
                        // monthly_value:// Recommended if using Pendo Feedback
                        // planLevel:    // Optional
                        // planPrice:    // Optional
                        // creationDate: // Optional

                        // You can add any additional account level key-values here,
                        // as long as it's not one of the above reserved names.
                    }
                });
            });
        }
    });
})('ff238906-0395-41ed-74d0-5cf719062755');


async function GetVisitorAndAccountIdsAsync() {
    const response = await fetch(
        "/PatientAccess/Default.aspx/GetVisitorAndAccountIds",
        {
            method: 'POST',
            headers: {
                "Content-type": "application/json; charset=UTF-8"
            }
        }
    );

    if (!response.ok) {
        alert("An error occurred while getting visitor and account ids.");
    }
    const data = await response.json(); // Extracting data as a JSON Object from the response
    return data;
}

function GetVisitorAndAccountIds(callback) {
    GetVisitorAndAccountIdsAsync().then(data => {
        callback(data);
    });
}


async function GetCanTrackEventsAsync() {
    const response = await fetch(
        "/PatientAccess/Default.aspx/CanTrackEvents",
        {
            method: 'POST',
            headers: {
                "Content-type": "application/json; charset=UTF-8"
            }
        }
    );

    if (!response.ok) {
        alert("An error occurred while getting data on whether event data can be tracked.");
    }
    const data = await response.json(); // Extracting data as a JSON Object from the response
    return data;
}

function GetCanTrackEvents(callback) {
    GetCanTrackEventsAsync().then(data => {
        callback(data);
    });
}