jQuery(document).ready(function (e) {
    function t(a, n) {
        e.ajax({
            type: "POST",
            url: t4aTwintVars.ajaxUrl,
            data: a,
            success: function (e) {
                var s = JSON.parse(e);
                if (s.status)
                    (p.close(),
                        n.prop("disabled", !1),
                        n.removeClass("loading"),
                        new TwntToast(s.message, "success"),
                        new l(s));
                else {
                    if (p.closed) return (n.prop("disabled", !1), void n.removeClass("loading"));
                    (h++,
                        setTimeout(function () {
                            t(a, n);
                        }, 3e3));
                }
            },
            error: function (e) {
                return p.closed
                    ? (n.prop("disabled", !1), void n.removeClass("loading"))
                    : (h++,
                      void setTimeout(function () {
                          t(a, n);
                      }, 3e3));
            },
        });
    }
    function a(t, a) {
        var n = "#" + m + "-integration-" + a;
        (e(n + " ." + m + "-setup-page").addClass("hidden"),
            e(n + " ." + m + "-setup-page-" + t).removeClass("hidden"),
            e(n + " ." + m + "-multi-steps .step").removeClass("is-active"),
            e(n + " ." + m + "-multi-step-" + t).addClass("is-active"),
            e(n + " ." + m + "-setup-assistant-btn").addClass("hidden"),
            t > 1 && e(n + " ." + m + "-back-btn").removeClass("hidden"),
            "direct" === a
                ? (5 == t && e(n + " ." + m + "-close-btn").removeClass("hidden"),
                  t < 3 && e(n + " ." + m + "-next-btn").removeClass("hidden"))
                : (4 == t && e(n + " ." + m + "-close-btn").removeClass("hidden"),
                  t < 4 && e(n + " ." + m + "-next-btn").removeClass("hidden")));
    }
    function n() {
        (e("#" + m + "-setup-assistant-field").addClass("hidden"),
            e("#" + m + "-settings-fields").removeClass("hidden"),
            e("#" + m + "-start-setup-button").removeClass("hidden"),
            e("#mainform .woocommerce-save-button").removeClass("hidden"),
            e("button[name='save']").show());
    }
    function s() {
        if ("twnt" === e("#twnttw_settings_integration option:selected").val()) {
            (e("#" + m + "-api-true").hide(), e("#" + m + "-api-false").hide());
            var t = e("#twnttw_settings_api_token"),
                a = t.val();
            if (a.length) {
                (t.prop("disabled", !0), e("#twnt_tw-api_token-loader").show());
                var n = {
                    action: "t4a_twint_ajax",
                    twnt_tw_action: "check_api_connection",
                    security: t4aTwintVars.ajaxNonce,
                    token: a,
                };
                e.ajax({
                    type: "POST",
                    url: t4aTwintVars.ajaxUrl,
                    data: n,
                    success: function (a) {
                        var n = JSON.parse(a);
                        (n.status
                            ? (e("#" + m + "-api-true").show(), e("#" + m + "-api-false").hide())
                            : (e("#" + m + "-api-true").hide(), e("#" + m + "-api-false").show()),
                            t.prop("disabled", !1),
                            e("#twnt_tw-api_token-loader").hide());
                    },
                    error: function (a) {
                        (e("#" + m + "-api-true").hide(),
                            e("#" + m + "-api-false").show(),
                            t.prop("disabled", !1),
                            e("#twnt_tw-api_token-loader").hide());
                    },
                });
            }
        }
    }
    function i() {
        (e("#twnt_tw-upload_file_button-wrapper").hide(), e("#twnt_tw-certificate-loader").show());
    }
    function o() {
        (e("#twnt_tw-upload_file_button-wrapper").show(), e("#twnt_tw-certificate-loader").hide());
    }
    function r(t) {
        (i(),
            e.when(
                e
                    .post(t4aTwintVars.ajaxUrl, {
                        twnt_tw_action: "enroll",
                        action: "t4a_twint_ajax",
                        security: t4aTwintVars.ajaxNonce,
                        environment: t,
                    })
                    .then(function (e) {
                        o();
                        var t = JSON.parse(e);
                        1 == t.status ? new TwntToast(t.message, "success") : new TwntToast(t.message, "error");
                    }),
            ));
    }
    function c(t) {
        var a = t.find(":selected");
        a.length && (e("." + t.data("conditional-group")).hide(), e(a.data("conditional-show")).show());
    }
    function d(t) {
        if (t.is(":visible")) {
            var a = e(t.data("conditional-show")),
                n = e(t.data("conditional-hide"));
            t.is(":checked")
                ? a.length
                    ? e(t.data("conditional-show")).show()
                    : n.length && e(t.data("conditional-hide")).hide()
                : a.length
                  ? e(t.data("conditional-show")).hide()
                  : n.length && e(t.data("conditional-hide")).show();
        }
    }
    var m = t4aTwintVars.prefix;
    e("." + m + "-btn-show-onload").css("visibility", "visible");
    var l = function (e) {
        ((this.actions = {}), this.handleResponse(e));
    };
    l.prototype = {
        handleResponse: function (t) {
            if (
                ("undefined" != typeof t.replace && this.replace(t.replace.element, t.replace["with"]),
                "undefined" != typeof t.replaceText && this.replaceText(t.replaceText.element, t.replaceText["with"]),
                "undefined" != typeof t.remove && this.remove(t.remove),
                "undefined" != typeof t.html && this.html(t.html),
                "undefined" != typeof t.text && this.text(t.text),
                "undefined" != typeof t.action && this.callAction(t.action),
                "undefined" != typeof t.attr && this.attr(t.attr),
                "undefined" != typeof t.addClass && this.addClass(t.addClass),
                "undefined" != typeof t.removeClass && this.removeClass(t.removeClass),
                "undefined" != typeof t.data && this.data(t.data),
                "undefined" != typeof t.append && this.append(t.append.to, t.append.element),
                "undefined" != typeof t.hide && this.hide(t.hide),
                "undefined" != typeof t.show)
            ) {
                var a = null;
                ("undefined" != typeof t.showScrollTo &&
                    null != t.showScrollTo &&
                    "null" != t.showScrollTo &&
                    (a = t.showScrollTo),
                    this.show(t.show, a));
            }
            ("undefined" != typeof t.after && this.after(t.after),
                "undefined" != typeof t.redirect && this.redirect(t.redirect),
                "undefined" != typeof t.scrollTo && this.scrollTo(t.scrollTo),
                "undefined" != typeof t.errors && this.errors(t.errors));
            var n = this;
            e.each(this.actions, function (e, a) {
                "undefined" != typeof t.action && t.action == e && n.callAction(e);
            });
        },
        scrollTo: function (t) {
            e("html, body").animate({ scrollTop: e(t.target).offset().top }, 500);
        },
        replace: function (t, a) {
            e(t).replaceWith(a);
        },
        replaceText: function (e, t) {
            e.text(t);
        },
        remove: function (e) {
            e.remove();
        },
        append: function (t, a) {
            e(t).append(a);
        },
        html: function (t) {
            e.each(t, function (t, a) {
                e(a.element).html(a.content);
            });
        },
        text: function (t) {
            e.each(t, function (t, a) {
                e(a.element).text(a.text);
            });
        },
        attr: function (t) {
            e.each(t, function (t, a) {
                e(a.elem).attr(a.attr, a.val);
            });
        },
        addClass: function (t) {
            e.each(t, function (t, a) {
                e(a.elem).addClass(a["class"]);
            });
        },
        removeClass: function (t) {
            e.each(t, function (t, a) {
                e(a.elem).removeClass(a["class"]);
            });
        },
        hide: function (t) {
            e.each(t, function (t, a) {
                e(a).hide();
            });
        },
        show: function (t, a) {
            if (
                (e.each(t, function (t, a) {
                    e(a).show();
                }),
                null != a)
            ) {
                var n = e(a);
                "undefined" != typeof n && e("html, body").animate({ scrollTop: n.offset().top }, 500);
            }
        },
        after: function (t) {
            e.each(t, function (t, a) {
                e(a.element).after(a.insert);
            });
        },
        redirect: function (e) {
            window.location = e;
        },
        errors: function (t) {
            e.each(t, function (t, a) {
                var n = a.container,
                    s = a.errors;
                (0 === t && e(n).empty(),
                    e.each(s, function (t, a) {
                        e(n).append("<p>" + a + "</p>");
                    }));
            });
        },
        data: function (t) {
            e.each(t, function (t, a) {
                e(a.elem).data(a.attr, a.val);
            });
        },
        addAction: function (e, t) {
            this.actions[e] = t;
        },
        callAction: function (e) {
            this.actions.hasOwnProperty(e) && this.actions[e]();
        },
    };
    var p,
        u = e("#" + m + "-setup-assistant-wrapper").data("page"),
        h = 0,
        f = e("#" + m + "-setup-assistant-field");
    (f.length && !f.hasClass("hidden") && e("#mainform .woocommerce-save-button").addClass("hidden"),
        e(document).on("click", "." + m + "-connect-store-button", function (a) {
            a.preventDefault();
            var n = e(this);
            (n.prop("disabled", !0), n.addClass("loading"));
            var s = e("#" + m + "-setup-assistant-wrapper").data("setup-key"),
                i = t4aTwintVars.restUrl + "setup/" + s;
            (console.log(i),
                (p = window.open(
                    t4aTwintVars.apiUrl + "/dashboard/service/twint/connect1?s=" + encodeURI(i),
                    "twnt-twint-setup",
                    "width=800,height=1000,left=150,top=200,toolbar=0,status=0,",
                )),
                t(
                    {
                        action: "t4a_twint_ajax",
                        twnt_tw_action: "update_stores_setup_status",
                        page: u,
                        security: t4aTwintVars.ajaxNonce,
                    },
                    n,
                ));
        }),
        e(document).on("click", "." + m + "-load-stores-button", function (t) {
            t.preventDefault();
            var a = e(this);
            (a.prop("disabled", !0), a.addClass("loading"));
            var n = {
                action: "t4a_twint_ajax",
                twnt_tw_action: "load_twint_stores",
                page: u,
                security: t4aTwintVars.ajaxNonce,
            };
            e.ajax({
                type: "POST",
                url: t4aTwintVars.ajaxUrl,
                data: n,
                success: function (e) {
                    var t = JSON.parse(e);
                    (a.prop("disabled", !1),
                        a.removeClass("loading"),
                        new TwntToast(t.message, t.status ? "success" : "error"),
                        new l(t));
                },
                error: function (e) {
                    new TwntToast(t4aTwintVars.texts.errorInResponse, "error");
                },
            });
        }),
        e(document).on("click", "." + m + "-next-btn, .twnt_tw-enroll_register", function (t) {
            t.preventDefault();
            var n = e(this)
                    .closest("." + m + "-integration-container")
                    .data("integration"),
                s = "#" + m + "-integration-" + n;
            e(s + " ." + m + "-setup-assistant-errors").empty();
            var i = e(s + " ." + m + "-setup-page-" + u),
                o = e(this);
            (o.prop("disabled", !0), o.addClass("loading"));
            var r = {
                action: "t4a_twint_ajax",
                twnt_tw_action: "save_setup_step",
                form: i.find("select, textarea, input").serialize(),
                page: u,
                security: t4aTwintVars.ajaxNonce,
                integration: n,
            };
            e.ajax({
                type: "POST",
                url: t4aTwintVars.ajaxUrl,
                data: r,
                success: function (e) {
                    (o.prop("disabled", !1), o.removeClass("loading"));
                    var t = JSON.parse(e);
                    (t.status && (a(++u, n), new TwntToast(t.message, "success")), new l(t));
                },
                error: function (e) {
                    (o.prop("disabled", !1), new TwntToast("error in request", "error"));
                },
            });
        }),
        e(".twnt_tw-certificate_upload_button").click(function (t) {
            t.preventDefault();
            var n = e(this)
                    .closest("." + m + "-integration-container")
                    .data("integration"),
                s = "#" + m + "-integration-" + n;
            e(s + " ." + m + "-setup-assistant-errors").empty();
            var i = e(this).data("environment"),
                o = wp.media.editor.send.attachment,
                r = e(this);
            return (
                (wp.media.editor.send.attachment = function (t, s) {
                    (e(r).prev().val(s.id), (wp.media.editor.send.attachment = o));
                    var c = {
                        action: "t4a_twint_ajax",
                        twnt_tw_action: "save_setup_step",
                        form: e.param({ attachment_id: s.id, environment: i }),
                        page: u,
                        security: t4aTwintVars.ajaxNonce,
                        integration: n,
                    };
                    e.ajax({
                        type: "POST",
                        url: t4aTwintVars.ajaxUrl,
                        data: c,
                        success: function (e) {
                            var t = JSON.parse(e);
                            (t.status && (a(++u, n), new TwntToast(t.message, "success")), new l(t));
                        },
                        error: function (e) {
                            (r.prop("disabled", !1), new TwntToast("error in request", "error"));
                        },
                    });
                }),
                wp.media.editor.open(r),
                !1
            );
        }),
        e(document).on("click", ".twnt_tw-switch-integration-settings", function (t) {
            t.preventDefault();
            var a = e(this).data("integration"),
                n = "twnt" === a || confirm(t4aTwintVars.texts.confirmSwitchIntegrationDirect);
            n && e("#twnttw_settings_integration").val(a).trigger("change");
        }),
        e(document).on("click", ".twnt_tw-switch-integration", function (t) {
            t.preventDefault();
            var n = e(this).data("integration"),
                s = "twnt" === n || confirm(t4aTwintVars.texts.confirmSwitchIntegrationDirect);
            if (s) {
                var i = e(this);
                (i.prop("disabled", !0), i.addClass("loading"), (u = 1));
                var o = {
                    action: "t4a_twint_ajax",
                    twnt_tw_action: "cancel_setup",
                    page: u,
                    security: t4aTwintVars.ajaxNonce,
                };
                e.ajax({
                    type: "POST",
                    url: t4aTwintVars.ajaxUrl,
                    data: o,
                    success: function (t) {
                        var s = JSON.parse(t);
                        (s.status &&
                            (e("." + m + "-integration-container").addClass("hidden"),
                            e("#" + m + "-integration-" + n).removeClass("hidden"),
                            a(u, n)),
                            i.prop("disabled", !1),
                            i.removeClass("loading"));
                    },
                    error: function (e) {
                        (i.prop("disabled", !1), i.removeClass("loading"), new TwntToast("error in request", "error"));
                    },
                });
            }
        }),
        e(document).on("click", "#twnt_tw-cancel-setup-button", function (t) {
            t.preventDefault();
            var a = confirm(t4aTwintVars.texts.confirmCloseSetupAssistant);
            if (a) {
                var s = e(this);
                (s.prop("disabled", !0), s.addClass("loading"), (u = 1));
                var i = {
                    action: "t4a_twint_ajax",
                    twnt_tw_action: "cancel_setup",
                    page: u,
                    security: t4aTwintVars.ajaxNonce,
                };
                e.ajax({
                    type: "POST",
                    url: t4aTwintVars.ajaxUrl,
                    data: i,
                    success: function (t) {
                        var a = JSON.parse(t);
                        (a.status &&
                            (e("." + m + "-integration-container").addClass("hidden"),
                            e("#" + m + "-integration-twnt").removeClass("hidden"),
                            n(),
                            e("#mainform .woocommerce-save-button").removeClass("hidden")),
                            s.prop("disabled", !1),
                            s.removeClass("loading"));
                    },
                    error: function (e) {
                        (s.prop("disabled", !1), s.removeClass("loading"), new TwntToast("error in request", "error"));
                    },
                });
            }
        }),
        e(document).on("click", "#twnt_tw-setup-restart-button", function (t) {
            t.preventDefault();
            var n = confirm(t4aTwintVars.texts.confirmRestartSetupAssistant);
            if (n) {
                var s = e(this);
                (s.prop("disabled", !0), s.addClass("loading"), (u = 1));
                var i = {
                    action: "t4a_twint_ajax",
                    twnt_tw_action: "cancel_setup",
                    page: u,
                    security: t4aTwintVars.ajaxNonce,
                };
                e.ajax({
                    type: "POST",
                    url: t4aTwintVars.ajaxUrl,
                    data: i,
                    success: function (t) {
                        var n = JSON.parse(t);
                        (n.status &&
                            (e("." + m + "-integration-container").addClass("hidden"),
                            e("#" + m + "-integration-twnt").removeClass("hidden"),
                            a(u, "twnt")),
                            s.prop("disabled", !1),
                            s.removeClass("loading"));
                    },
                    error: function (e) {
                        (s.prop("disabled", !1), new TwntToast("error in request", "error"));
                    },
                });
            }
        }),
        e(document).on("click", "#" + m + "-integration-selector-twnt", function (t) {
            (t.preventDefault(),
                e("#" + m + "-integration-selector").addClass("hidden"),
                e("#" + m + "-integration-twnt").removeClass("hidden"),
                (u = 1),
                a(1, "twnt"));
        }),
        e(document).on("click", "#" + m + "-integration-selector-direct", function (t) {
            (t.preventDefault(),
                e("#" + m + "-integration-selector").addClass("hidden"),
                e("#" + m + "-integration-direct").removeClass("hidden"),
                (u = 1),
                a(u, "direct"));
        }),
        e(document).on("click", "." + m + "-editor-skip-button", function (t) {
            t.preventDefault();
            var n = e(this)
                .closest("." + m + "-integration-container")
                .data("integration");
            a(++u, n);
        }),
        e(document).on("click", "." + m + "-back-btn", function (t) {
            t.preventDefault();
            var n = e(this)
                .closest("." + m + "-integration-container")
                .data("integration");
            (e(this)
                .closest("." + m + "-setup-assistant-errors")
                .empty(),
                a(--u, n));
        }),
        e(document).on("click", "." + m + "-close-btn", function (e) {
            (e.preventDefault(), n());
        }),
        e(document).on("click", "." + m + "-start-setup-button", function (t) {
            (t.preventDefault(),
                e("#" + m + "-setup-assistant-field").removeClass("hidden"),
                e("#" + m + "-settings-fields").addClass("hidden"),
                (u = 1),
                a(u, "twnt"),
                a(u, "direct"),
                e("button[name='save']").hide());
        }),
        e("#" + m + "-setup-complete").length &&
            (e("#" + m + "-setup-complete").data("value") || e("button[name='save']").hide()),
        e(document).on("click", ".twnt_tw-modal-close", function (t) {
            (t.preventDefault(), e("#twnt_tw-modal").addClass("hidden"));
        }),
        e(document).on("click", "#twnt_tw-check-logs-btn", function (t) {
            (t.preventDefault(), e("#twnt_tw-modal").removeClass("hidden"));
        }),
        e(document).mouseup(function (t) {
            var a = e("#twnt_tw-modal #twnt_tw-modal-content");
            a.is(t.target) || 0 !== a.has(t.target).length || e("#twnt_tw-modal").addClass("hidden");
        }),
        e(document).on("click", "." + m + "-enable-gateway-toggle", function (t) {
            t.preventDefault();
            var a = e(this);
            if (!a.hasClass("woocommerce-input-toggle--loading")) {
                a.addClass("woocommerce-input-toggle--loading");
                var n = { action: "t4a_twint_ajax", twnt_tw_action: "toggle_enable", security: t4aTwintVars.ajaxNonce };
                e.ajax({
                    type: "POST",
                    url: t4aTwintVars.ajaxUrl,
                    data: n,
                    success: function (e) {
                        var t = JSON.parse(e);
                        (new l(t), a.removeClass("woocommerce-input-toggle--loading"));
                    },
                    error: function (e) {
                        a.removeClass("woocommerce-input-toggle--loading");
                    },
                });
            }
        }));
    var w = null;
    (e("#twnttw_settings_integration").length && ((w = e("#twnttw_settings_api_token").val()), s()),
        e(document).on("change", "#twnttw_settings_integration", function (e) {
            s();
        }),
        e(document).on("blur", "#twnttw_settings_api_token", function (t) {
            var a = e("#twnttw_settings_api_token").val();
            w !== a && ((w = a), s());
        }),
        e(document).on("click", "#twnt_tw-check-order-status-btn", function (t) {
            t.preventDefault();
            var a = e(this),
                n = a.data("order-id");
            (a.prop("disabled", !0),
                e.when(
                    e
                        .post(t4aTwintVars.ajaxUrl, {
                            action: "t4a_twint_ajax",
                            twnt_tw_action: "check_order_status",
                            security: t4aTwintVars.ajaxNonce,
                            order_id: n,
                        })
                        .then(function (e) {
                            a.prop("disabled", !1);
                            var t = JSON.parse(e);
                            1 == t.status
                                ? new TwntToastSticky(t.message, "success")
                                : new TwntToastSticky(t.message, "error");
                        }),
                ));
        }),
        e(document).on("click", "#twnt_tw-check-alias-status-btn", function (t) {
            t.preventDefault();
            var a = e(this),
                n = a.data("order-id");
            (a.prop("disabled", !0),
                e.when(
                    e
                        .post(t4aTwintVars.ajaxUrl, {
                            action: "t4a_twint_ajax",
                            twnt_tw_action: "is_alias_active",
                            security: t4aTwintVars.ajaxNonce,
                            order_id: n,
                        })
                        .then(function (e) {
                            a.prop("disabled", !1);
                            var t = JSON.parse(e);
                            (1 == t.status
                                ? new TwntToastSticky(t.message, "success")
                                : new TwntToastSticky(t.message, "error"),
                                new l(t));
                        }),
                ));
        }),
        e(document).on("click", "#twnt_tw-twnt-check-transaction-status-btn", function (t) {
            t.preventDefault();
            var a = e(this),
                n = a.data("order-id"),
                s = a.data("transaction-id");
            (a.prop("disabled", !0),
                e.when(
                    e
                        .post(t4aTwintVars.ajaxUrl, {
                            action: "t4a_twint_ajax",
                            twnt_tw_action: "twnt_check_transaction_status",
                            security: t4aTwintVars.ajaxNonce,
                            transaction_id: s,
                            order_id: n,
                        })
                        .then(function (e) {
                            a.prop("disabled", !1);
                            var t = JSON.parse(e);
                            (1 == t.status ? new TwntToast(t.message, "success") : new TwntToast(t.message, "error"),
                                new l(t));
                        }),
                ));
        }),
        e(document).on("click", "#twnt_tw-twnt-capture-transaction-btn", function (t) {
            t.preventDefault();
            var a = confirm(t4aTwintVars.texts.confirmSettlement);
            if (a) {
                var n = e(this),
                    s = n.data("order-id"),
                    i = n.data("transaction-id");
                (n.prop("disabled", !0),
                    e.when(
                        e
                            .post(t4aTwintVars.ajaxUrl, {
                                action: "t4a_twint_ajax",
                                twnt_tw_action: "twnt_settle_transaction",
                                security: t4aTwintVars.ajaxNonce,
                                order_id: s,
                                transaction_id: i,
                            })
                            .then(function (e) {
                                n.prop("disabled", !1);
                                var t = JSON.parse(e);
                                (1 == t.status
                                    ? new TwntToast(t.message, "success")
                                    : new TwntToast(t.message, "error"),
                                    new l(t));
                            }),
                    ));
            }
        }),
        e(document).on("click", "#twnt_tw-twnt-cancel-transaction-btn", function (t) {
            t.preventDefault();
            var a = confirm(t4aTwintVars.texts.confirmCancellation);
            if (a) {
                var n = e(this),
                    s = n.data("order-id"),
                    i = n.data("transaction-id");
                (n.prop("disabled", !0),
                    e.when(
                        e
                            .post(t4aTwintVars.ajaxUrl, {
                                action: "t4a_twint_ajax",
                                twnt_tw_action: "twnt_cancel_transaction",
                                security: t4aTwintVars.ajaxNonce,
                                order_id: s,
                                transaction_id: i,
                            })
                            .then(function (e) {
                                n.prop("disabled", !1);
                                var t = JSON.parse(e);
                                (1 == t.status
                                    ? new TwntToastSticky(t.message, "success")
                                    : new TwntToastSticky(t.message, "error"),
                                    new l(t));
                            }),
                    ));
            }
        }),
        e(document).on("click", "#twnt_tw-settle-transaction-btn", function (t) {
            t.preventDefault();
            var a = confirm(t4aTwintVars.texts.confirmSettlement);
            if (a) {
                var n = e(this),
                    s = n.data("order-id");
                (n.prop("disabled", !0),
                    e.when(
                        e
                            .post(t4aTwintVars.ajaxUrl, {
                                action: "t4a_twint_ajax",
                                twnt_tw_action: "settle_transaction",
                                security: t4aTwintVars.ajaxNonce,
                                order_id: s,
                            })
                            .then(function (e) {
                                n.prop("disabled", !1);
                                var t = JSON.parse(e);
                                (1 == t.status
                                    ? new TwntToast(t.message, "success")
                                    : new TwntToast(t.message, "error"),
                                    new l(t));
                            }),
                    ));
            }
        }),
        e(document).on("click", "#twnt_tw-cancel-transaction-btn", function (t) {
            t.preventDefault();
            var a = confirm(t4aTwintVars.texts.confirmCancellation);
            if (a) {
                var n = e(this),
                    s = n.data("order-id");
                (n.prop("disabled", !0),
                    e.when(
                        e
                            .post(t4aTwintVars.ajaxUrl, {
                                action: "t4a_twint_ajax",
                                twnt_tw_action: "cancel_transaction",
                                security: t4aTwintVars.ajaxNonce,
                                order_id: s,
                            })
                            .then(function (e) {
                                n.prop("disabled", !1);
                                var t = JSON.parse(e);
                                (1 == t.status
                                    ? new TwntToastSticky(t.message, "success")
                                    : new TwntToastSticky(t.message, "error"),
                                    new l(t));
                            }),
                    ));
            }
        }),
        e(".twnt_tw-create_certificate_button").click(function (t) {
            t.preventDefault();
            var a = e(this).data("environment");
            e.when(
                e
                    .post(t4aTwintVars.ajaxUrl, {
                        action: "t4a_twint_ajax",
                        twnt_tw_action: "save_file",
                        security: t4aTwintVars.ajaxNonce,
                        environment: a,
                        content: e("textarea[name='t4a_twint_settings_cert_" + a + "']").serialize(),
                    })
                    .then(function (t) {
                        var n = JSON.parse(t);
                        1 == n.status
                            ? (new TwntToast(n.message, "success"),
                              e(".twnt_tw-certificate-upload-wrapper ." + a).show())
                            : new TwntToast(n.message, "error");
                    }),
            );
        }),
        e(".twnt_tw-upload_file_button").click(function (t) {
            t.preventDefault();
            var a = e(this).data("environment"),
                n = wp.media.editor.send.attachment,
                s = e(this);
            return (
                (wp.media.editor.send.attachment = function (t, i) {
                    (e(s).prev().val(i.id),
                        (wp.media.editor.send.attachment = n),
                        e.when(
                            e
                                .post(t4aTwintVars.ajaxUrl, {
                                    action: "t4a_twint_ajax",
                                    twnt_tw_action: "convert",
                                    security: t4aTwintVars.ajaxNonce,
                                    attachment_id: i.id,
                                    environment: a,
                                })
                                .then(function (e) {
                                    var t = JSON.parse(e);
                                    1 == t.status
                                        ? new TwntToast(t.message, "success")
                                        : new TwntToast(t.message, "error");
                                }),
                        ));
                }),
                wp.media.editor.open(s),
                !1
            );
        }),
        e("input[name='t4a_twint_check_certificate_expiration']").click(function (t) {
            t.preventDefault();
            var a = e(this);
            (a.prop("disabled", !0),
                e.when(
                    e
                        .post(t4aTwintVars.ajaxUrl, {
                            twnt_tw_action: "check_expiry",
                            action: "t4a_twint_ajax",
                            security: t4aTwintVars.ajaxNonce,
                        })
                        .then(function (e) {
                            a.prop("disabled", !1);
                            var t = JSON.parse(e);
                            1 == t.status ? new TwntToast(t.message, "success") : new TwntToast(t.message, "error");
                        }),
                ));
        }),
        e("input[name='t4a_twint_renew_certificate']").click(function (e) {
            e.preventDefault();
            var t = new TwntTwDialog(
                t4aTwintVars.texts.renewCertificateTitle,
                t4aTwintVars.texts.renewCertificateText,
                "renew_certificate",
            );
            t.open();
        }),
        e("input[name='t4a_twint_enroll_register']").click(function (t) {
            (t.preventDefault(), r(e("select#twnttw_settings_environment option:selected").val()));
        }));
    var v = function (t) {
        this.phpVars = t;
        var a = this;
        (e("." + this.phpVars.prefix + "-license-check-btn").click(function (e) {
            (e.preventDefault(), a.check_license());
        }),
            e("." + this.phpVars.prefix + "-license-activate-btn").click(function (e) {
                (e.preventDefault(), a.activate_license());
            }),
            e("." + this.phpVars.prefix + "-license-deactivate-btn").click(function (e) {
                (e.preventDefault(), a.deactivate_license());
            }),
            e("#" + this.phpVars.prefix + "-ignore-notice").click(function (e) {
                (e.preventDefault(), a.ignore_notice());
            }));
    };
    if (
        ((v.prototype.show_loader = function () {
            (e("#" + this.phpVars.prefix + "-license-field-wrapper").hide(),
                e("#" + this.phpVars.prefix + "-loader").show());
        }),
        (v.prototype.hide_loader = function () {
            (e("#" + this.phpVars.prefix + "-loader").hide(),
                e("#" + this.phpVars.prefix + "-license-field-wrapper").show());
        }),
        (v.prototype.check_license = function () {
            this.show_loader();
            var t = this;
            e.ajax({
                type: "POST",
                url: t.phpVars.ajaxUrl,
                data: {
                    action: t.phpVars.prefix + "_ajax_license",
                    security: t.phpVars.ajaxNonce,
                    license_action: "check",
                },
                success: function (a) {
                    var n = e.parseJSON(a);
                    (n.status
                        ? new TwntToastSticky(n.message, "success")
                        : (e("#" + t.phpVars.prefix + "-license-invalid").show(),
                          e("#" + t.phpVars.prefix + "-license-valid").hide(),
                          new TwntToastSticky(n.message, "error")),
                        t.hide_loader());
                },
                error: function (e) {
                    (new TwntToast(t.phpVars.texts.licenseCheckFailed, "error"), t.hide_loader());
                },
            });
        }),
        (v.prototype.activate_license = function () {
            this.show_loader();
            var t = this,
                a = e("#" + this.phpVars.prefix + "_license_key").val();
            e.ajax({
                type: "POST",
                url: t.phpVars.ajaxUrl,
                data: {
                    action: t.phpVars.prefix + "_ajax_license",
                    security: t.phpVars.ajaxNonce,
                    license: a,
                    license_action: "activate",
                },
                success: function (n) {
                    var s = e.parseJSON(n);
                    (s.status
                        ? (new TwntToast(s.message, "success"),
                          e("#" + t.phpVars.prefix + "-license-invalid").hide(),
                          e("#" + t.phpVars.prefix + "-license-valid").show(),
                          e("#" + t.phpVars.prefix + "-current-license-key").text(a))
                        : new TwntToastSticky(s.message, "error"),
                        t.hide_loader());
                },
                error: function (e) {
                    (new TwntToast(t.phpVars.texts.licenseActivationFailed, "error"), t.hide_loader());
                },
            });
        }),
        (v.prototype.deactivate_license = function () {
            this.show_loader();
            var t = this;
            e.ajax({
                type: "POST",
                url: t.phpVars.ajaxUrl,
                data: {
                    action: t.phpVars.prefix + "_ajax_license",
                    security: t.phpVars.ajaxNonce,
                    license_action: "deactivate",
                },
                success: function (a) {
                    var n = e.parseJSON(a);
                    (n.status
                        ? (new TwntToast(n.message, "success"),
                          e("#" + t.phpVars.prefix + "-license-invalid").show(),
                          e("#" + t.phpVars.prefix + "-license-valid").hide())
                        : new TwntToastSticky(n.message, "error"),
                        t.hide_loader());
                },
                error: function (e) {
                    (new TwntToast(t.phpVars.texts.licenseActivationFailed, "error"), t.hide_loader());
                },
            });
        }),
        (v.prototype.ignore_notice = function () {
            var t = this;
            e.when(
                e
                    .post(t.phpVars.ajaxUrl, {
                        action: t.phpVars.prefix + "_ajax_license",
                        security: t.phpVars.ajaxNonce,
                        license_action: "notice",
                    })
                    .then(function (a) {
                        var n = e.parseJSON(a);
                        n.status &&
                            e("#" + t.phpVars.prefix + "-ignore-notice")
                                .closest(".error")
                                .hide();
                    }, "json"),
            );
        }),
        e("." + t4aTwintVars.prefix + "-license-check-btn").length > 0)
    ) {
        new v(t4aTwintVars);
    }
    (e("#twnttw_settings_environment")
        .change(function () {
            "test" === e("#twnttw_settings_environment option:selected").val()
                ? (e(".twnttw-prod-settings").closest("tr").hide(), e(".twnttw-test-settings").closest("tr").show())
                : (e(".twnttw-test-settings").closest("tr").hide(), e(".twnttw-prod-settings").closest("tr").show());
        })
        .trigger("change"),
        "function" != typeof TwntToast &&
            (window.TwntToast = function (t, a) {
                ((this.message = t),
                    e("body").append("<p id='twnt-toast' class='twnt-toast show " + a + "'>" + t + "</p>"),
                    setTimeout(function () {
                        (e("#twnt-toast").removeClass("show"),
                            setTimeout(function () {
                                e("#twnt-toast").remove();
                            }, 1e3));
                    }, 5e3));
            }),
        "function" != typeof TwntToastSticky &&
            ((window.TwntToastSticky = function (t, a) {
                this.message = t;
                var n = e("#twnt-toast-sticky");
                (n.length && n.remove(),
                    e("body").append(
                        "<div id='twnt-toast-sticky' class='twnt-toast-sticky show " +
                            a +
                            "'><div class='twnt-toast-sticky-inner'><p>" +
                            t +
                            "</p><button id='twnt-toast-close' class='" +
                            a +
                            "'>x</button></div></div>",
                    ));
            }),
            e(document).on("click", "#twnt-toast-close", function (t) {
                (t.preventDefault(), e("#twnt-toast-sticky").removeClass("show"), e("#twnt-toast-sticky").remove());
            })),
        (window.TwntTwDialog = function (t, a, n) {
            ((this.text = a), (this.title = t), (this.type = n));
            var s = this;
            ("renew_certificate" === n && (this.callback = this.renewCertificate),
                e(document).on("click", "#twnt_tw-modal-cancel", function (e) {
                    (e.preventDefault(), s.close());
                }));
        }),
        (TwntTwDialog.prototype = {
            open: function () {
                (e("#twnt_tw-modal-content .title").empty(),
                    e("#twnt_tw-modal-content .text").empty(),
                    e("#twnt_tw-modal-content .title").html(this.title),
                    e("#twnt_tw-modal-content .text").html(this.text),
                    e("#twnt_tw-modal").removeClass("hidden"));
                var t = this;
                e(document).on("click", "#twnt_tw-modal-confirm", function (a) {
                    (a.preventDefault(), t.callback(e(this)));
                });
            },
            close: function () {
                (e("#twnt_tw-modal").addClass("hidden"), e(document).off("click", "#twnt_tw-modal-confirm"));
            },
            renewCertificate: function (t) {
                var a = this;
                (t.prop("disabled", !0),
                    e.when(
                        e.post(t4aTwintVars.ajaxUrl, a.getRequestParams("renew_certificate")).then(function (e) {
                            (t.prop("disabled", !1), a.handleResponse(e), a.close());
                        }, "json"),
                    ));
            },
            getRequestParams: function (e) {
                return { action: "t4a_twint_ajax", twnt_tw_action: e, security: t4aTwintVars.ajaxNonce };
            },
            handleResponse: function (e) {
                var t = JSON.parse(e);
                1 == t.status ? new TwntToast(t.message, "success") : new TwntToast(t.message, "error");
            },
        }),
        e(function () {
            e(".twnt_tw-chosen-select").chosen();
        }),
        e(".twnt_tw-conditional-select-field").each(function () {
            c(e(this));
        }),
        e(document).on("change", ".twnt_tw-conditional-select-field", function (t) {
            (c(e(this)), e(".twnt_tw-chosen-select").chosen());
        }),
        e(".twnt_tw-conditional-checkbox-field").each(function () {
            d(e(this));
        }),
        e(document).on("change", ".twnt_tw-conditional-checkbox-field", function (t) {
            d(e(this));
        }));
});
