jQuery(document).ready(function() {
    const bypass_design_upload = W2PShopProductPageData.bypass_design_upload;
    const is_cart_updated = W2PShopProductPageData.is_cart_updated === '1';
    const cart_url = W2PShopProductPageData.cart_url;
    const cart_item_key = W2PShopProductPageData.cart_item_key;

    if (is_cart_updated) {
        window.location.href = cart_url;
        return;
    }

    setTimeout(function () {
        if (cart_item_key) {
            // Updating existing cart item.
            W2PShopProduct.set_product_data('cart_item_key', cart_item_key);
            W2PShopProduct.add_cart_item_key_to_form(cart_item_key);
            W2PShopProduct.get_add_to_cart_button().removeAttr('name').removeAttr('value');

            const add_to_cart_element = jQuery('input[name="add-to-cart"]');
            if (add_to_cart_element.length > 0) {
                add_to_cart_element.remove();
            }
        }
    }, 1000);

    function verify_w2pshop_data_on_product_submit(e) {
        let gosendex_upload_data = []; let udraw_designer_data = null; let pdf_lib_data = null;
        if (typeof uDrawConnect !== 'undefined') {
            udraw_designer_data = W2PShopProduct.get_product_data('udraw_designer_data');
        }

        if ( typeof w2p_bm !== 'undefined' ) {
            pdf_lib_data = W2PShopProduct.get_product_data('pdfeditor_unique_id');
        }

        if ( typeof w2pshop_gosendex.__get_gse_uploaded_artwork === 'function' ) {
            gosendex_upload_data = w2pshop_gosendex.__get_gse_uploaded_artwork();
        }

        //If no design data present, display a warning message otherwise proceed Adding to Cart
        if ( gosendex_upload_data.length === 0 && pdf_lib_data === null && udraw_designer_data === null ) {
            e.preventDefault();
            e.stopPropagation();
            W2PShopCommon.display_confirm_message(
                'Are you sure?',
                "Do you wish to Add to Cart without an Uploaded File / Design?",
                'Add To Cart',
                function() {
                    W2PShopProduct.get_add_to_cart_button().off('click', verify_w2pshop_data_on_product_submit);
                    W2PShopProduct.get_add_to_cart_button().click();
                }
            );
        }
    }

    if (bypass_design_upload === 'yes') {
        setTimeout(function() {
            W2PShopProduct.get_add_to_cart_button().show();
        }, 1000);
        W2PShopProduct.get_add_to_cart_button().on('click', verify_w2pshop_data_on_product_submit);
    }
});