(function () {
    "use strict";

    if (!window.Swal) {
        return;
    }

    function getIcon(message) {
        var text = String(message || "").toLowerCase();

        if (text.indexOf("success") !== -1 || text.indexOf("succesfully") !== -1 || text.indexOf("successfully") !== -1) {
            return "success";
        }

        if (text.indexOf("fail") !== -1 || text.indexOf("wrong") !== -1 || text.indexOf("error") !== -1 || text.indexOf("sorry") !== -1) {
            return "error";
        }

        if (text.indexOf("not authorized") !== -1 || text.indexOf("insufficient") !== -1 || text.indexOf("warning") !== -1) {
            return "warning";
        }

        return "info";
    }

    window.myGoldAlert = function (message, redirectUrl) {
        return Swal.fire({
            title: "My Gold PLT",
            text: String(message || ""),
            icon: getIcon(message),
            confirmButtonText: "OK",
            confirmButtonColor: "#d4af37",
            background: "#ffffff",
            customClass: {
                popup: "mygold-alert-popup",
                title: "mygold-alert-title",
                confirmButton: "mygold-alert-button"
            }
        }).then(function () {
            if (redirectUrl) {
                window.location.href = redirectUrl;
            }
        });
    };

    window.alert = function (message) {
        window.myGoldAlert(message);
    };

    function decodeQuotedText(value) {
        return String(value || "")
            .replace(/\\'/g, "'")
            .replace(/\\"/g, '"')
            .replace(/\\\\/g, "\\");
    }

    function getConfirmMessage(handlerText) {
        var match = String(handlerText || "").match(/confirm\s*\(\s*(['"])(.*?)\1\s*\)/i);
        return match ? decodeQuotedText(match[2]) : "Do you want to proceed?";
    }

    function rememberSubmitter(event) {
        var target = event.target;

        if (!target || !target.form) {
            return;
        }

        if (target.matches && target.matches("button[type='submit'], button:not([type]), input[type='submit'], input[type='image']")) {
            target.form.__myGoldSubmitter = target;
        }
    }

    function submitConfirmedForm(form, submitter) {
        if (submitter && submitter.name) {
            var hidden = document.createElement("input");
            hidden.type = "hidden";
            hidden.name = submitter.name;
            hidden.value = submitter.value || "";
            form.appendChild(hidden);
        }

        HTMLFormElement.prototype.submit.call(form);
    }

    function enhanceConfirmForms() {
        var forms = document.querySelectorAll("form[onsubmit*='confirm'], form[onsubmit*=\"confirm\"]");

        Array.prototype.forEach.call(forms, function (form) {
            var handlerText = form.getAttribute("onsubmit") || "";
            var message = getConfirmMessage(handlerText);

            form.removeAttribute("onsubmit");
            form.addEventListener("submit", function (event) {
                var submitter = event.submitter || form.__myGoldSubmitter;

                event.preventDefault();

                Swal.fire({
                    title: "My Gold PLT",
                    text: message,
                    icon: "question",
                    showCancelButton: true,
                    confirmButtonText: "Yes, proceed",
                    cancelButtonText: "Cancel",
                    confirmButtonColor: "#d4af37",
                    cancelButtonColor: "#6c757d",
                    background: "#ffffff",
                    customClass: {
                        popup: "mygold-alert-popup",
                        title: "mygold-alert-title",
                        confirmButton: "mygold-alert-button"
                    }
                }).then(function (result) {
                    if (result.isConfirmed || result.value) {
                        submitConfirmedForm(form, submitter);
                    }
                });
            });
        });
    }

    document.addEventListener("click", rememberSubmitter, true);

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", enhanceConfirmForms);
    } else {
        enhanceConfirmForms();
    }
}());
