<reponame>uktrade/irap<filename>frontend/src/gds.js<gh_stars>0export function gdsOptions() {
  const options = document.querySelectorAll("[data-gds-options]");

  for (const container of options) {
    const fieldName = container.dataset.gdsOptions;

    const inputs = container.querySelectorAll(`input[name=${fieldName}]`);

    // Closure
    function handleChange(event) {
      const values = [];

      for (const input of inputs) {
        if (input.checked) {
          values.push(input.value);
        }

        const conditional = container.querySelector(`#conditional-${input.id}`);

        if (!conditional) {
          continue;
        }

        conditional.style.display = input.checked ? "block" : "none";
      }

      container.dataset.gdsOptionsValue = values;
    }

    for (const input of inputs) {
      input.addEventListener("change", handleChange);
    }

    handleChange();
  }
}
