/* exported setTemplateData */

/* global setupDropdownFilters */

// Remove this after PackageController#rdiff moves to DiffListComponent
$(function ($) {
  $('body').on('click', '.expand-diffs', function () {
    var forPackage = $(this).data('package');
    var details = $('details.card[data-package="' + forPackage + '"]');
    details.attr('open', 'open');
  });

  $('body').on('click', '.collapse-diffs', function () {
    var forPackage = $(this).data('package');
    var details = $('details.card[data-package="' + forPackage + '"]');
    details.attr('open', null);
  });
});

$(document).ready(function() {
  setupDropdownFilters();
  $('.btn-more').click(function() {
    var moreInfo = $('.more_info');
    moreInfo.toggleClass('d-none');
    $(this).text(moreInfo.hasClass('d-none') ? 'more info' : 'less info');
  });
});

async function setTemplateData(url) {
  try {
    var templateDescriptions = document.getElementsByClassName("template-description");
    Array.from(templateDescriptions).forEach((desc) => {
      desc.innerHTML = '';
    });
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`Response failed with status: ${response.status}`);
    }

    const result = await response.json();
    result.forEach((object) => {
      var field = document.getElementById(`description-${object.name}`);
      field.innerText = object.description;
    });
  } catch (error) {
    console.error(error.message);
  }
}
