function fense_bpvt_scan_template(n) {
    const message = (n.message && n.message.length > 0)
        ? n.message
        : "Proxy or VPN detected - Please disable to access this website!";

    const link = (n.link && n.link.length > 0)
        ? n.link
        : "https://fense.in/how-it-works";

    const learnMore = (n.disable_link === "yes")
        ? `<a href="${link}">Learn How to disable</a>`
        : "";

    return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name='robots' content='max-image-preview:large, noindex, follow' />
    <title>${message}</title>
    <style type="text/css">
        html{background:#f1f1f1}body{background:#fff;border:1px solid #ccd0d4;color:#444;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;margin:2em auto;padding:1em 2em;max-width:700px;box-shadow:0 1px 1px rgba(0,0,0,.04)}#error-page{margin-top:50px}#error-page p,#error-page .wp-die-message{font-size:14px;line-height:1.5;margin:25px 0 20px}a{color:#2271b1}a:hover,a:active{color:#135e96}
    </style>
</head>
<body id="error-page">
    <div class="wp-die-message"><p>${message} ${learnMore}</p></div>
    <p><a href="javascript:history.back()">&laquo; Back</a></p>
</body>
</html>`;
}

(function () {
    // Guard: prevent double execution
    if (window.FENSE_BPVT_SCAN_RUNNING) return;
    window.FENSE_BPVT_SCAN_RUNNING = true;

    // Guard: localized data must exist
    if (typeof fense_bpvt_scan === "undefined") return;

    const ajaxUrl = fense_bpvt_scan.ajax;
    const key = fense_bpvt_scan.key;

    function makeForm(action) {
        const fd = new FormData();
        fd.append("action", key + action);
        fd.append("url", window.location.href);
        return fd;
    }

    function post(action) {
        return fetch(ajaxUrl, {
            method: "POST",
            body: makeForm(action),
            credentials: "same-origin",
        }).then(function (r) {
            return r.json();
        });
    }

    post("SaveMode").then(function (res) {
        if (res.success && res.data && res.data.status) {
            document.open();
            document.write(fense_bpvt_scan_template(res.data));
            document.close();
        }
    });

    // // Step 1: get cookies (token + nonce)
    // post("GetSaveMode").then(function (res) {
    //     if (!res.success) return;
    //
    //     // Step 2: run detection
    //
    // });

}());
