jQuery(document).ready(function () {
  let lastBlock = null;

  jQuery(document).on('mouseover', '.wp-block-acf-rdg-team-schedules', function (event) {
    lastBlock = jQuery(this);
  });

  jQuery.ajaxSetup({
    beforeSend: function (event, data) {
      if (2 !== data.data.split('field_65c24').length) {
        return true;
      }

      if (data.data.includes('field_65c24e23461a3')) {
        lastBlock.find('select[name*="field_65c24f0a461a4"]').empty().trigger('change');
        lastBlock.find('select[name*="field_65c24f21461a5"]').empty().trigger('change');
        lastBlock.find('select[name*="field_65c24f34461a6"]').empty().trigger('change');

        lastBlock.find('select[name*="field_65c24f0a461a4"]').data('select2').results.clear();
        lastBlock.find('select[name*="field_65c24f21461a5"]').data('select2').results.clear();
        lastBlock.find('select[name*="field_65c24f34461a6"]').data('select2').results.clear();
      } else if (data.data.includes('field_65c24f0a461a4')) {
        lastBlock.find('select[name*="field_65c24f21461a5"]').empty().trigger('change');
        lastBlock.find('select[name*="field_65c24f34461a6"]').empty().trigger('change');

        lastBlock.find('select[name*="field_65c24f21461a5"]').data('select2').results.clear();
        lastBlock.find('select[name*="field_65c24f34461a6"]').data('select2').results.clear();

        data.data += '&customData=' + lastBlock.find('select[name*="field_65c24e23461a3"]').val();
      } else if (data.data.includes('field_65c24f21461a5')) {
        data.data += '&customData=' + lastBlock.find('select[name*="field_65c24f0a461a4"]').val();
      } else if (data.data.includes('field_65c24f34461a6')) {
        data.data += '&customData=' + lastBlock.find('select[name*="field_65c24f0a461a4"]').val();
      }

      return true;
    }
  });
});
