


jQuery(document).ready(function ($) {


    jQuery(document).on('wc_backbone_modal_loaded', function (evt, target) {
        if ('wc-modal-shipping-method-settings' === target) {
            wcFreeShippingShowHideVendorField();

        }
    });


    jQuery(document.body).on('onkeyup', '#woocommerce_free_shipping_elex_extra_field_assigned_vendor', function () {
        wcFreeShippingShowHideVendorField();
    });

    function wcFreeShippingShowHideVendorField() {
        var form = jQuery('#woocommerce_free_shipping_elex_extra_field_assigned_vendor').closest('form');
        var VendorField = jQuery('#woocommerce_free_shipping_elex_extra_field_assigned_vendor', form).closest('fieldset');
        const allOtherLabelElementsP = VendorField.prev('label');


        VendorField.css('display', 'block');

        jQuery(allOtherLabelElementsP).css('display', 'block');
        VendorField.show();

        jQuery(allOtherLabelElementsP).show();


    }



    jQuery(document.body).on('wc_backbone_modal_loaded', { view: this }, function () {


        var id = jQuery('.shipping_method_for_vendors_assignment').val();
        jQuery(".shipping_method_for_vendors_assignment").select2({
            allowClear: true,
            placeholder: "Search Vendor",
            dataType: 'json',
            minimumInputLength: 2,
            // multiple: true,

            ajax: {
                url: elex_mv_coupon_ajaxurl_object.ajax_url,
                type: 'GET',

                data: function (params) {


                    var query = {
                        vendor_search: params.term.replace(/\s+/g, " ").trim(),
                        action: 'elex_mv_api_endpoint_for_vendors',
                        nonce: elex_mv_coupon_ajaxurl_object.nonce,
                    }
                    return query;

                },
                processResults: function (response) {

                    return {
                        results: response.data.map(function (store) {

                            return {
                                text: store.post_title,
                                id: store.ID,
                            }
                        }),
                    };
                }
            }



        });
    });




})

