(function ($, Drupal) {
  Drupal.behaviors.advisoryCouncils = {
    attach(context) {
      $(once("advisoryCouncils", "html", context).forEach((element) => {
        (($) => {
          $(document).ready(() => {
            $(".nac-all-meetings-block-view table")
              .removeClass("tablesaw tablesaw-swipe")
              .removeAttr("id data-tablesaw-mode");
            $(".nac-all-meetings-block-view .tablesaw-bar").remove();

            // To make dropdown list items accessible to screen readers
            $("#edit_field_meeting_date_value_chosen .chosen-search-input").attr(
              "aria-label",
              `Filter by Year ${$(
                "#edit-field-meeting-date-value option:eq(0)"
              ).text()}`
            );
            $("#edit_field_council_list_value_chosen .chosen-search-input").attr(
              "aria-label",
              `Filter by Advisory Council ${$(
                "#edit-field-council-list-value option:eq(0)"
              ).text()}`
            );

            $(
              "#views-exposed-form-nac-meetings-nac-meetings-page .chosen-drop ul.chosen-results"
            ).on("DOMSubtreeModified", () => {
              $(".chosen-drop ul.chosen-results").attr("role", "listbox");
              $("#edit_field_meeting_date_value_chosen .chosen-search-input").attr(
                "aria-label",
                "Filter by Year"
              );
              $("#edit_field_council_list_value_chosen .chosen-search-input").attr(
                "aria-label",
                "Filter by Advisory Council"
              );
              $(
                "#views-exposed-form-nac-meetings-nac-meetings-page .chosen-drop ul.chosen-results li"
              ).each((i, el) => {
                $(el).attr("id", $(el).text());
                $(el).attr("role", "option");
              });
            });

            $("#edit_field_meeting_date_value_chosen .chosen-search-input").on(
              "keydown",
              (e) => {
                if (e.keyCode === 40 || e.keyCode === 38) {
                  $(
                    "#edit_field_meeting_date_value_chosen .chosen-drop ul.chosen-results li"
                  ).each((i, el) => {
                    if ($(el).hasClass("highlighted")) {
                      $(
                        "#edit_field_meeting_date_value_chosen .chosen-search-input"
                      ).attr("aria-activedescendant", $(el).attr("id"));
                    }
                  });
                }
              }
            );

            $("#edit_field_council_list_value_chosen .chosen-search-input").on(
              "keydown",
              (e) => {
                if (e.keyCode === 40 || e.keyCode === 38) {
                  $(
                    "#edit_field_council_list_value_chosen .chosen-drop ul.chosen-results li"
                  ).each((i, el) => {
                    if ($(el).hasClass("highlighted")) {
                      $(
                        "#edit_field_council_list_value_chosen .chosen-search-input"
                      ).attr("aria-activedescendant", $(el).attr("id"));
                    }
                  });
                }
              }
            );
          });
        })(jQuery);
      }));
    }
  }
})(jQuery, Drupal);