/**
 * Comment Security Layer - Settings tab UX bindings.
 *
 * Confirms destructive actions and applies subtle UI affordances.
 * Localized strings come from `pdtCommentSecurityI18n`.
 */
(function ($) {
  'use strict';

  if (typeof window.pdtCommentSecurityI18n === 'undefined') {
    window.pdtCommentSecurityI18n = {
      confirmClear: 'Are you sure you want to clear all logs? This cannot be undone.',
      confirmReset: 'Reset all Comment Security settings to default?'
    };
  }

  function bindConfirmations() {
    $(document).on('click', '[data-confirm-action="clear"]', function (event) {
      var msg = window.pdtCommentSecurityI18n.confirmClear;
      // eslint-disable-next-line no-alert
      if (!window.confirm(msg)) {
        event.preventDefault();
      }
    });

    $(document).on('click', '[data-confirm-action="reset"]', function (event) {
      var msg = window.pdtCommentSecurityI18n.confirmReset;
      // eslint-disable-next-line no-alert
      if (!window.confirm(msg)) {
        event.preventDefault();
      }
    });
  }

  function bindSliderState() {
    // Visually fade dependent number fields when their toggle is off.
    var pairs = [
      { toggle: 'pdt_cs[honeypot_enabled]', target: '#pdt_cs_honeypot_field_name' },
      { toggle: 'pdt_cs[timing_enabled]', target: '#pdt_cs_timing_min_seconds, #pdt_cs_timing_max_seconds' },
      { toggle: 'pdt_cs[flood_enabled]', target: '#pdt_cs_flood_per_minute, #pdt_cs_flood_per_hour' },
      { toggle: 'pdt_cs[score_enabled]', target: '#pdt_cs_score_threshold_spam, #pdt_cs_score_threshold_drop' }
    ];

    pairs.forEach(function (pair) {
      var $toggle = $('input[name="' + pair.toggle + '"]');
      var $targets = $(pair.target);
      if (!$toggle.length || !$targets.length) {
        return;
      }
      var sync = function () {
        var on = $toggle.is(':checked');
        $targets.prop('disabled', !on).css('opacity', on ? '' : '0.6');
      };
      $toggle.on('change', sync);
      sync();
    });
  }

  $(function () {
    bindConfirmations();
    bindSliderState();
  });
})(jQuery);
