facetwp disable dropdown conditionally

<script>
        (function($) {
            /**
             * creates a parent -> child -> grandchild dependency to disable facets
             */
            $(document).on('facetwp-loaded', function() {
                if ('undefined' !== typeof FWP.facets['product_categories'] && FWP.facets['product_categories'].length < 1 ) {
                    $( '.facetwp-facet-product_tags select, .facetwp-facet-rating select').prop('disabled', 'disabled');
                } else if ('undefined' !== typeof FWP.facets['product_tags'] && FWP.facets['product_tags'].length < 1 ) {
                    $( '.facetwp-facet-rating select').prop('disabled', 'disabled');
	            }
            });
            /**
             * resets the facets when the "parent" is deselected
             */
            $(document).on('facetwp-refresh', function() {
                if ( 'undefined' !== typeof FWP.facets['product_categories'] && FWP.facets['product_categories'].length < 1 ) {
                    FWP.facets['product_tags'] = [];
                    FWP.facets['rating'] = [];
                } else if ( 'undefined' !== typeof FWP.facets['product_tags'] && FWP.facets['product_tags'].length < 1 ) {
                    FWP.facets['rating'] = [];
                }
            });
        })(jQuery);
</script>