(function ($) {
  "use strict";

  function refreshAccessCards() {
    $(".polanger-shield-access-card input[type='checkbox']").each(function () {
      $(this).closest(".polanger-shield-access-card").toggleClass("is-selected", $(this).is(":checked"));
    });
  }

  function refreshLevelCards() {
    $(".polanger-shield-global-demo-level-option input[type='radio']").each(function () {
      $(this).closest(".polanger-shield-global-demo-level-option").toggleClass("is-selected", $(this).is(":checked"));
    });
  }

  function refreshVisibility() {
    var enabled = $("#pdt-shield-global-demo-enabled").is(":checked");
    var level = $(".polanger-shield-global-demo-level-option input[type='radio']:checked").val() || "balanced";

    $("#polanger-shield-global-demo-config").toggleClass("is-visible", enabled);
    $("#polanger-shield-global-demo-strict-options").toggleClass("is-visible", enabled && level === "strict");
    refreshLevelCards();
    refreshAccessCards();
  }

  $(function () {
    $(document).on("change", "#pdt-shield-global-demo-enabled", refreshVisibility);
    $(document).on("change", ".polanger-shield-global-demo-level-option input[type='radio']", refreshVisibility);
    $(document).on("change", ".polanger-shield-access-card input[type='checkbox']", refreshAccessCards);

    refreshVisibility();
  });
})(jQuery);
