jQuery(document).ready(function() {
    $mo = jQuery, $mo("#enter_otp_field").hide(), focusChecks()
});
let button = '<button type="button" onclick="sendOTP()" class="button miniorange_wc_otp_token_submit" id="miniorange_wc_otp_token_submit">' + mowcac.buttontext + "</button>";
var loadingImageHtml = "<img src='" + mowcac.imgURL + "'>",
    otpFieldHtml = button + "<div id='mo_message' hidden='' style='background-color: #f7f6f7;padding: 1em 2em 1em 3.5em;'>" + loadingImageHtml + "</div>";

function focusChecks() {
    "phone" == mowcac.otpType ? $mo("form.edit-account #billing_phone").on("change paste keyup", function() {
        $mo(this).val() === mowcac.fieldValue ? removeOTPField() : (insertOTPField(), $mo("#enter_otp_field").insertAfter("#mo_message").show())
    }) : $mo("form.edit-account #account_email").on("change paste keyup", function() {
        $mo(this).val() === mowcac.fieldValue ? removeOTPField() : (insertOTPField(), $mo("#enter_otp_field").insertAfter("#mo_message").show())
    })
}

function sendOTP() {
    if ("phone" == mowcac.otpType) var userInput = $mo("#billing_phone").val();
    else userInput = $mo("#account_email").val();
    $mo("#mo_message").empty(), $mo("#mo_message").append(loadingImageHtml), $mo("#mo_message").show(), $mo.ajax({
        url: mowcac.siteURL,
        type: "POST",
        data: {
            action: mowcac.generateURL,
            user_input: userInput,
            security: mowcac.nonce
        },
        crossDomain: !0,
        dataType: "json",
        success: function(response) {
            "success" == response.result ? ($mo("#mo_message").empty(), $mo("#mo_message").append(response.message), $mo("#mo_message").css("border-top", "3px solid green")) : ($mo("#mo_message").empty(), $mo("#mo_message").append(response.message), $mo("#mo_message").css("border-top", "3px solid red"))
        },
        error: function(errorResponse) {
            $mo("#mo_message").empty(), $mo("#mo_message").append(errorResponse.message), $mo("#mo_message").css("border-top", "3px solid red")
        }
    })
}

function removeOTPField() {
    $mo("#miniorange_wc_otp_token_submit").remove(), $mo("#enter_otp_field").hide(), $mo("#mo_message").remove()
}

function insertOTPField() {
    0 == $mo("#miniorange_wc_otp_token_submit").length && ("phone" == mowcac.otpType ? $mo(otpFieldHtml).insertAfter("#billing_phone_field") : $mo(otpFieldHtml).insertAfter("#account_email"))
}