jQuery(document).ready((function() {
    $mo = jQuery;
    setTimeout((function() {
        if (typeof mowccheckoutnew === "undefined") {
            return;
        }
        var fieldId = mowccheckoutnew.field;
        var fieldSelector = "#" + fieldId;
        var $field = $mo(fieldSelector);
        if ($field.length === 0 && mowccheckoutnew.otpType === "phone") {
            var $try = $mo("#billing-phone");
            if ($try.length) {
                fieldId = "billing-phone";
                fieldSelector = "#billing-phone";
                $field = $try;
            }
        }
        var $fieldParent = $field.length
            ? ($field.closest(".form-row, .wc-block-components-text-input, .wc-block-components-phone-number").length
                ? $field.closest(".form-row, .wc-block-components-text-input, .wc-block-components-phone-number")
                : $field.parent())
            : $mo();
        var loadingImageHtml = "<div style='display:table;text-align:center;'><img decoding='async' src=" + mowccheckoutnew.imgURL + "></div>";
        var $buttonHtml = $mo("<input type='button' id='miniorange_otp_token_submit' style='width: 100%; padding: 3%; margin: 2% 0%;' class='components-button wc-block-components-button wp-element-button contained' value='Send OTP'/><div id='mo_message' style='background-color: #f7f6f7; display:none; padding: 1em 2em 1em 3.5em;'></div>");
        if ($fieldParent.length) {
            $buttonHtml.insertAfter($fieldParent);
        }
        var effectiveFieldSelector = fieldSelector;
        $mo("#miniorange_otp_token_submit").on("click", (function() {
            var userInput = $mo(effectiveFieldSelector).val(),
                phoneVerifyInput = $mo("input[name=phone_verify]"),
                messageElement = $mo("#mo_message");
            messageElement.empty(), messageElement.append(loadingImageHtml), messageElement.show(), $mo.ajax({
                url: mowccheckoutnew.siteURL,
                type: "POST",
                data: {
                    user_phone: userInput,
                    user_email: userInput,
                    action: mowccheckoutnew.gaction,
                    security: mowccheckoutnew.nonce,
                    otpType: mowccheckoutnew.otpType
                },
                crossDomain: !0,
                dataType: "json",
                success: function(response) {
                    var msg = (response && response.message) ? String(response.message) : "";
                    "success" == response.result ? (messageElement.empty(), messageElement.text(msg), messageElement.css({
                        "background-color": "#dbfff7",
                        color: "#008f6e"
                    }), phoneVerifyInput.focus(), $mo("#mo_verify_otp_fields").length ? ($mo("#mo_verify_otp_fields").show(), $mo("#miniorange_verify_otp_token").show()) : ($mo("<div class='wc-block-components-text-input' id='mo_verify_otp_fields'><input type='text' id='mo_otp_token' style='width: 100%; padding: 3%; margin: 2% 0%;' class='wc-block-components-button contained' aria-label='Enter OTP'/><label for='mo_otp_token'>Enter OTP</label></div><input type='button' id='miniorange_verify_otp_token' style='width: 100%; padding: 3%; margin: 2% 0%;' class='components-button wc-block-components-button wp-element-button contained' value='Verify OTP'/>").insertAfter("#mo_message"), $mo("#miniorange_verify_otp_token").on("click", (function() {
                        var userInputForVerify = $mo(effectiveFieldSelector).val(),
                            otpToken = $mo("#mo_otp_token").val(),
                            messageElementForVerify = $mo("#mo_message");
                        $mo.ajax({
                            url: mowccheckoutnew.siteURL,
                            type: "POST",
                            data: {
                                user_phone: userInputForVerify,
                                user_email: userInputForVerify,
                                action: mowccheckoutnew.vaction,
                                security: mowccheckoutnew.nonce,
                                otpType: mowccheckoutnew.otpType,
                                otp_token: otpToken
                            },
                            crossDomain: !0,
                            dataType: "json",
                            success: function(verifyResponse) {
                                var verifiedUserInput;
                                "success" == verifyResponse.result ? (messageElementForVerify.empty(), messageElementForVerify.hide(), $mo("#mo_verify_otp_fields").hide(), $mo("#miniorange_verify_otp_token").hide(), $mo("#miniorange_otp_token_submit").val("Verified ✔"), $mo("#mo_otp_token").val(""), verifiedUserInput = userInputForVerify, $mo(effectiveFieldSelector).on("keydown keyup", (function() {
                                    $mo(effectiveFieldSelector).val() !== verifiedUserInput ? $mo("#miniorange_otp_token_submit").val("Send OTP") : $mo("#miniorange_otp_token_submit").val("Verified ✔")
                                }))) : (messageElementForVerify.empty(), messageElementForVerify.text((verifyResponse && verifyResponse.message) ? String(verifyResponse.message) : "Invalid OTP."), messageElementForVerify.css({
                                    "background-color": "#ffefef",
                                    color: "#ff5b5b"
                                }), messageElementForVerify.show())
                            },
                            error: function(jqXHR, textStatus, errorThrown) {
                            }
                        })
                    })))) : (messageElement.empty(), messageElement.append(response.message), messageElement.css({
                        "background-color": "#ffefef",
                        color: "#ff5b5b"
                    }))
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    messageElement.empty();
                    var errMsg = "Unable to send OTP. Please try again.";
                    try {
                        if (jqXHR && jqXHR.responseText) {
                            var parsed = JSON.parse(jqXHR.responseText);
                            if (parsed && parsed.message) errMsg = parsed.message;
                        }
                    } catch (e) {}
                    messageElement.append(errMsg).css({ "background-color": "#ffefef", color: "#ff5b5b" }).show();
                }
            })
        }))
    }), 200)
}));