function hideUnhide(field) {
  var allSources = $('body').find('.export-source-option')
  hide(allSources)

  if (field.length > 0) {
    var selectedSource = $('.' + field)
    unhideSelected(selectedSource)
  }

  if (field === 'collection') {
    initCollectionSearchInputs();
  }
};

// hide all export_source
function hide(allSources) {
  allSources.addClass('hidden');
  allSources.find('#exporter_export_source').addClass('hidden').attr('type', 'hidden');
}

// unhide selected export_source
function unhideSelected(selectedSource) {
  selectedSource.removeClass('hidden').removeAttr('type');
  selectedSource.parent().removeClass('hidden').removeAttr('type');
};

// add the autocomplete javascript
function initCollectionSearchInputs() {
  $('[data-autocomplete]').each((function() {
    var elem = $(this)
    initSelect2(elem, elem.data('autocompleteUrl'))
  }))
}

function initSelect2(element, url) {
  element.select2({
    minimumInputLength: 2,
    initSelection : (row, callback) => {
      var data = {id: row.val(), text: row.val()};
      callback(data);
    },
    ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
      url: url,
      dataType: 'json',
      data: (term, page) => {
        return {
          q: term // search term
        };
      },
      results: function(data, page) {
        // parse the results into the format expected by Select2.
        // since we are using custom formatting functions we do not need to alter remote JSON data
        let results = data.map((obj) => {
          return { id: obj.id, text: obj.label[0] };
        })
        return { results: results };
      }
    }
  }).select2('data', null);
}